retrieving number of visible characters in label?

planarduelplanarduel Posts: 4Member
edited December 1969 in GUI
I have a label in which the text is too long for all of it to display; is there a way to check how many characters have been fit inside? I assume this number changes depending on the text since characters can be different sizes.. it seems like all the class methods are about the text independent of the label size.

Comments

  • bitwesbitwes Posts: 59Member
    You might be able to hack something together using the [tt]is_clipping[/tt] method.  You could check to see if it is clipping then loop through the text, removing a character until [tt]is_clipping[/tt] returns false.<br /><br />You could also use a fixed width font and just count the characters, but Courier doesn't look as cool as Comic Sans.<br /><br />Other languages have this (I've used a similar feature in Objective C before), so it's possible.  In my very brief googling I didn't see any posts covering the logic, only instructions on how to use methods that did it for you.
  • planarduelplanarduel Posts: 4Member
    on 1466293912:
    <br />You might be able to hack something together using the [tt]is_clipping[/tt] method.  You could check to see if it is clipping then loop through the text, removing a character until [tt]is_clipping[/tt] returns false.<br /><br />You could also use a fixed width font and just count the characters, but Courier doesn't look as cool as Comic Sans.<br /><br />Other languages have this (I've used a similar feature in Objective C before), so it's possible.  In my very brief googling I didn't see any posts covering the logic, only instructions on how to use methods that did it for you.<br />
    <br /><br />Unfortunately I think the [tt]is_clipping[/tt] method (if you mean [tt]is_clipping_text[/tt], which is all I could find) returns whether or not you would clip text if it exceeded the box size, rather than if the current text is actually clipping. (I did try to implement the hacky solution, but it didn't work due to this.)
  • bitwesbitwes Posts: 59Member
    I should have seen that coming, given how the other label functions pair up.  I just found that the [tt]Font[/tt] class has [tt]Vector2 get_string_size( String string ) const[/tt].  Says it returns a string size taking kerning and advance into account.  I don't know what those two words mean, but I do know that most string sizes aren't expressed as a Vector, so You might be in luck there.  <br /><br />You could use an approach similar to the clipping one or the reverse, where you add characters until the x is too much, then back off one.  The darn logic is built into the label...it knows how to wrap text...just can't figure out how to get to it.
  • planarduelplanarduel Posts: 4Member
    I ended up doing something similar (checking the size of the string); I used [tt]get_line_count[/tt] and [tt]get_line_height[/tt] to check how tall the string was in the label (I'm using autowrapping) and compared that in a loop to the actual label height. Still not sure how one would do it without autowrapping though, it'd be nice if that got added in eventually.<br /><br />Thanks for you help! :)
  • bitwesbitwes Posts: 59Member
    Nice!  I like that approach.  It gets you the "word breaking" that you get from the auto-wrap.  This would be a great feature to add.  You should check the enhancement list and add it if it's not there.<br />
Sign In or Register to comment.