Is there a way to, given a string, get the substring that would fit into a FlxText given size restrictions?

23 views
Skip to first unread message

MouseCaneta

unread,
Oct 27, 2015, 9:55:47 AM10/27/15
to HaxeFlixel
Well, the question is basically in the title.

Sam Bellman

unread,
Oct 28, 2015, 5:03:34 AM10/28/15
to HaxeFlixel
I know this method will work in flash and it seems to work in cpp, I can't swear to other targets though. Also it's a a bit heavy, so I wouldn't do it if you have a very long string. If you want to use it on a multiline textfield just modify it so it looks at the height instead of the width.
public static function cutTextWidth(tf:FlxText, maxWidth:Float = -1, dotDotDot:Bool = false):Void
{
maxWidth = maxWidth > 0 ? maxWidth : tf.fieldWidth;
if (tf.textField.textWidth+5 > maxWidth)
{
var copy:String = tf.textField.text;
var initialCopy:String = copy;
while (tf.textField.textWidth + 5 > maxWidth && copy.length > 0)
{
copy = copy.substr(0, copy.length - 1);
tf.textField.text = dotDotDot ? copy + "..." : copy;
}
if (initialCopy != copy) tf.dirty = true;
}
}

MouseCaneta

unread,
Oct 28, 2015, 6:03:05 PM10/28/15
to HaxeFlixel
Thanks a lot! I'll give it a try.
Reply all
Reply to author
Forward
0 new messages