Word Wrap
BlitzMax Forums/BlitzMax Beginners Area/Word Wrap
| ||
Is there any easy way to get word wrapping? |
| ||
Other than calculating it yourself? |
| ||
SuperStrict Function wordwrap$(txt$,linelength:Int) Local words$[]=txt.split(" ") Local out$="" Local i:Int=0 Local thisline:Int=0 Local word$ For word$=EachIn words If thisline+Len(word)>linelength out:+"~n" thisline=0 EndIf If thisline out:+" " out:+word thisline:+Len(word) Next Return out End Function Print wordwrap("Hello there, this is a very long sentence which will be wrapped to make it fit inside a certain sized box. Another sentence on the end.",20) |
| ||
Thanks Warpy! I changed it a bit to make fit my needs. Function WordWrap:String(txt:String, X:Int, Y:Int, Width:Int, Height:Int) Local words:String[] = txt.split(" ") Local out:String Local i:Int Local word:String For word = EachIn words If TextWidth(Out + Word) < Width - 1 Then Out:+" " + Word Else Out = Trim(Out) DrawText(Out, x, y + i) i:+TextHeight(Out) Out = Word EndIf If i + TextHeight(Out) >= Height Then Return 0 Next If Out Then DrawText(Out, x, y + i) End Function |
| ||
Hmm actually, there are still a few problems with this that I can't seem to solve on my own... First of all, if the word is really long it (of course) doesn't cut it off. If TextWidth(word) >= Width - 1 Then For Local N:Int = Len(word) To 0 Step - 1 If TextWidth(Left(word, N)) < Width - 1 Then Print Left(word, N) Exit EndIf Next End If That detects it but I'm not sure how to insert the rest of that into the array... Also, I need a new line character in there that forces a new line. Any help? :3 |