I used this to take some arbitrary text put it into a hidden edit field (pEditField) so that I could determine the individual line strings to draw.
kcr is a constant that maps to Chr(13)
pText is the string that needs splitting.
pMaxWidth is used to resize the hidden edit field to the maximum width of the area I will be drawing into later on.
pEditField is the hidden edit field.
I imagine you won’t need pText & pMaxWidth.
[code]Function WrapText(pText As String, pMaxWidth As Int32, pEditField As TextArea) As String()
Dim theResult(-1) As String
Dim nextLine As Int32
Dim lineNumber As Int32
Dim textLine As String
Dim i As Int32
Dim theChar As String
If pEditField.Width <> pMaxWidth Then
pEditField.Width = pMaxWidth
End If
pText = ReplaceLineEndings(pText, kcr)
pEditField.Text = pText
lineNumber = 0
textLine = “”
For i = 1 To Len(pText)
theChar = Mid(pText, i, 1)
'if the character is a carriage return then add the line and reset the entry field
'this prevents a problem under win32 where carriage returns seem to effect the wrapping
If theChar = kcr Then
theResult.Append(Trim(textLine))
textLine = ""
lineNumber = 0
pText = Mid(pText, i + 1)
pEditField.Text = pText
i = 0
Else
nextLine = pEditField.LineNumAtCharPos(i)
If nextLine > lineNumber Then
'osx seems to report the change one character too late (bug?) so we need to break at the previous character
#If TargetMacOS Then
theResult.Append(Trim(Left(textLine, Len(textLine) - 1)))
i = i - 1
theChar = Mid(pText, i, 1)
#Else
theResult.Append(Trim(textLine))
#EndIf
textLine = ""
lineNumber = nextLine
End If
textLine = textLine + theChar
End If
Next
If Len(textLine) > 0 Then
theResult.Append(Trim(textLine))
End If