What is the best way to wrap text?
Monkey Forums/Monkey Programming/What is the best way to wrap text?
| ||
| Hi monkey community. I was wondering if there was an easy way to wrap text to a certain width with CreateText? Thanks |
| ||
| Split your text into words, after that you loop all words and increase the x-position that changes with the TextWidth() of the word. [monkeycode] splittedText:String[] = yourText.Split (" ") Local xpos:Float Local line:Int For Local i:Int = 0 Until splittedText.Length xpos += TextWidth (splittedText[i]) If xpos > YOUR_SPECIFIED_LINE_WIDTH xpos = 0 'Reset the xpos to 0 line += 1 'Go to next line End DrawText (splittedText[i], xpos, line*YOUR_LINE_HEIGHT) Next [/monkeycode] That's untested but I hope you get the idea. |
| ||
| Or use Fontmachine. |
| ||
| I downloaded fontmachine but I don't see any properties or funtions to wrap text. |
| ||
| I have looked for about a day trying to find an easy way to wrap text with fontmachine. Do I need to calculate things myself, and insert a new line, or is there an easiesr way? |
| ||
| For word wrapping when using FontMachine, Taiphoz added an overloaded method here to do it: http://www.monkeycoder.co.nz/Community/posts.php?topic=3207#33908 Also check out his extended BitMap class here: http://code.google.com/p/monkey-touch/source/browse/trunk/main.monkey [monkeycode]#Rem summary: Extension of Font Machine's BitmapFont Created so that others dont need to hack their font machine module. Should be removed soon once Ziggy writes his own more efficiant version. #END Class BitmapFont2 extends BitmapFont[/monkeycode] |
| ||
| Thanks, I'll try it out |
| ||
| Coming back to this, I realized that if you add a new line, it breaks the code and the text backs up on itself. Any solutions (besides not using ~n :)? |
| ||
| Hey, I figured out a nice solution to make wrapping work with "~n". My change to the code adds two small lines. Just replace the SplitLines Method with below. Hope this helps someone:
Method SplitLines:String[] (_text:String, _width:Int)
'new addition
_text = _text.Replace("~n", "~n ")
Local Tai_textdata:String[] = _text.Split(" ")
'temp vars for building lines.
Local Tai_tmptext:String=""
Local Tai_words:Int=Tai_textdata.Length()
Local Tai_textlines:String[Tai_words] 'its a fudge. but number of lines will possibly match number of words.
Local Tai_line:Int = 0
For Local Tai_word:Int = 0 To Tai_words-1
Tai_tmptext+=Tai_textdata[Tai_word]+" "
If Self.GetTxtWidth(Tai_tmptext) < _width And Tai_tmptext.Contains("~n") = False 'added new condition
Tai_textlines[Tai_line]=Tai_tmptext
Else
Tai_line+=1
Tai_tmptext=(Tai_textdata[Tai_word]+" ")
End If
Next
Return Tai_textlines
End Method
|
| ||
| ondesic, you mentioned "CreateText". Are you talking about fantomEngine here? Mojo does not have a CreateText command. If it is about fE, then look at the MultilineText example. |
| ||
| My original post started that way a while ago, but I am using FontMachine in the example above. |
| ||
| Sorry, i should have paid more attention to the timestamp. |
| ||
| Darn, what I would really like is to be able to use AngelFont with it's DrawHTML() method BUT alow the text to have "~n" in it. Right now even if I use the SimpleTextBox(), if I have a newline anywhere,the text is just drawn on one line over and over again overlapping itself and making the text a muttle. Any help would sure be appreciated. |
| ||
| Maybe contact Ziggy directly regarding Fontmachine. |
| ||
| ondesic - have you tried the latest version of AngelFont? http://www.monkeycoder.co.nz/Community/posts.php?topic=141 |
| ||
| Thank you. I saw that the first post was a year ago so I thought it was older. It seems to work now. Perfect!!! |