Word wrapping text without splitting words
Solution 1:
To do word wrapping of text, you need to split it into words on each space, then calculate how much space it will take from the beginning to each space in turn. When it goes above the area to fit in, you print out everything up to the pervious space then start again on the new line.
As the text size is determined by the font in use, you will need to query the drawing library to see how big each chunk will be. If using GDI, you can use the GetTextExtentPoint32()
function. If drawing to a VB6 picturebox before creating the JPEG, you can use the .TextWidth()
method.
Also note that the GDI DrawText()
function has an option to break on words automatically given a Rect.
You'll need to provide more information on how you're doing the drawing and creating the image for a more specific answer.
Solution 2:
Here is a function that should work
Private Function FormatString(ByVal StringToFormat As String, ByVal MaxLineLen As Integer) As String
Dim TempString As String
Dim Pos As Long
FormatString = ""
Pos = 1
While StringToFormat <> ""
If Len(StringToFormat) <= MaxLineLen Then
TempString = Trim(StringToFormat)
Else
TempString = Mid(StringToFormat, Pos, MaxLineLen + 1)
TempString = Trim(Left(TempString, InStrRev(TempString, " ")))
End If
FormatString = FormatString & TempString & vbCrLf
StringToFormat = LTrim(Right(StringToFormat, Len(StringToFormat) - Len(TempString)))
Wend
End Function