March 3, 2012 at 2:17 pm #2587
Hi, Another problem :)
When using this method on a string that has a space at the end
– For Example “Hello “
It returns the length of the string minus the space at the end
Any way to get it to return the total length?
ChrisMarch 3, 2012 at 2:23 pm #3312
I’ll take a look…March 3, 2012 at 10:47 pm #3315
Thought of a simple workaround if anyone else needed it
(Shouldn’t have taken me this long to figure out lol)
String = “Hello “
Width = Yourfont.GetTextWidth(String + “A”) – Yourfont.GetTextWidth(“A”)
result is length of the string including the trailing space :)
Simples :)April 22, 2012 at 3:39 pm #3376
@297crisc: Your method is not 100% acurate as it takes into account the spacing on last leter, and not its drawing size, so on script fonts it could be wrong. I’ve updated the FontMachine module to another one that provides proper testsize calculation, including white spaces at the end of the string.July 19, 2014 at 11:53 am #5717
I have the newest FontMachine module, but it isn’t working.
In file bitmapfont.monkey, starting from line 188:
If lastchar >= 0 And lastchar < faceChars.Length() Then if lastchar = 32 then 'Do nothing. We let the spacing at the end of the string. ElseIf faceChars[lastchar] <> Null Then twidth = twidth - faceChars[lastchar].drawingMetrics.drawingWidth twidth = twidth + faceChars[lastchar].drawingMetrics.drawingSize.x End If End If
So what you need, is metrics for a space. It does, however, include spaces once the space isn’t the one trailing.
“Hello World! ” is shorter than..
“Hello World !”
I’ve also noticed while making text inputs, that the cursor jumps back a little when I add a trailing space at the very end of the text.
But seriously, excluding spaces from so called “StringWidth”? No. One should never do that.
Spaces are valid part of a String.July 19, 2014 at 1:16 pm #5719
Looks like I can only edit my post once… Well, anyway, double-posting then!
If someone needs, here’s a working fix for trailing spaces:
If lastchar >= 0 And lastchar < faceChars.Length() Then If lastchar = 32 then ' Working space twidth = twidth + faceChars[lastchar].drawingMetrics.drawingWidth + Kerning.x ElseIf faceChars[lastchar] <> Null Then twidth = twidth - faceChars[lastchar].drawingMetrics.drawingWidth twidth = twidth + faceChars[lastchar].drawingMetrics.drawingSize.x End If End If
Replace lines 188-196 in bitmapfont.monkey with this code and it works fine.
And ziggy, please add this to the official FontMachine module.. My console-input will look stupid without this… ;P
You must be logged in to reply to this topic.