How do I set textarea height to total height of text

0 favourites
  • 8 posts
From the Asset Store
Cue in the war drums and battle cadences with Total War Game Music Loops!
  • Hi,

    Could anyone help me with setting up the height of textarea to total height amount of text within it?

    Thx

  • so you basically want to "wrap" your text? Meaning make it so it doesn't cut off or not display text if it is too long for the height you gave it in the editor?

    Never tried it before but here's an idea:

    Account for which font and how many "W"s (or your font's widest letter) would fit in the given width; check how many characters are in your text string (making sure to check for newlines) then just do the math... Also you need to figure how much height to add with each newline.

    if 50 is your letter width max and you have 150 characters (including spaces) you add (3 * lineheight) to your TextBox's height.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Will try What about enter's? Thanks!

  • yes, that's what I meant about "newlines" or "carriage returns". You need to count them as a full line by themselves.

    I'm not that great at string parsing in C2 but maybe the best way would be to replace all the "newline"s or "enters" with spaces (or any character for that matter) just so you can count all the characters to make the correct amount of lines.

    so let's say your TextBox's line height is 7 (pixels) and your line width is 20 (characters)...

    Set TextBox1 Text to ("This is my text." & newline & newline & "And this is a new paragraph.")
    Call Function AutoTextHeight("This is my text." & newline & newline & "And this is a new paragraph.")
    [/code:2m5u4q9r]
    and in the Function do this:
    
    [code:2m5u4q9r]
    Function AutoTextHeight-------------
    replace(Function.Param(0),newline,"XXXXXXXXXXXXXXXXXXXX")  //this will replace all your "enters" with Xs
    
    Set TextBox1 Height to (len(Function.Param(0) / 20) * 7 // divide by the linewidth and multiply by lineheight[/code:2m5u4q9r]
    
    you would need to pass the UID of the Text Object to "pick" it.. or you don't even need it in a function.  Just make sure to set the Text with the "enters" then make a copy of the string to replaces the "enters" for counting only. You don't want the actual Xs in your text.
  • I'm having problems with my suggestion.. now I'm not sure of the best way to do it.

    The first problem is my method requires monospacing (a font like courier). The second problem is for some reason C2 won't recognize "newline" as a carriage return (enter) in the replace() function. The ascii character is 0x13 or 'CR' but I don't know how to reference that in C2...

    also it seems the text object resize during runtime doesn't wrap the text...

    There must be a better way to do this..

    here is the capx I was working with...

  • jobel hey, I didn't get notification about your replies. Will look in the capx soon.

  • stuff

    not sure if this is better or not, but this is how i did it

  • megatronx yeah sorry I don't have a solution.. was hoping someone else could pitch in.. maybe trultz has the answer?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)