newText and determining if it actually fits in the text box

Does anyone have any experience working with text? I am creating a text box with a bounding rectangle so it wraps. Its all good mostly except on some platforms the text is too big for the box and gets cropped.

Is there any way to tell if this happens in code - then I can change the size of the text until its small enough to fit in the box? [import]uid: 158162 topic_id: 32904 reply_id: 332904[/import]

One sure way to get the exact size of text, is to create the text, and check it’s size…

If it’s too big, resize it (change font size, etc).

In the following case, I am creating a text object of a fixed width, and am later checking the height to see if it’s too big.

 local messageText = display.newText( messages[i].MESSAGE, 0, 0, rowWidth, 0, native.systemFont, 32 ) -- height 0, and system sets it to actual height generated   
  
 if( messageText.height \> 100 ) then -- Is it too big\>  
 -- change font size, regenerate text... do in a loop if you want to...  
 end  

I’m actually doing something different with my code – I’m measrung the height of my text, and then building some UI elements around it (tableView rows), but I’ve got a limit on how large they each will get… But anywho, getting the height of the text is key to a lot of the formatting.
[import]uid: 79933 topic_id: 32904 reply_id: 130755[/import]

One sure way to get the exact size of text, is to create the text, and check it’s size…

If it’s too big, resize it (change font size, etc).

In the following case, I am creating a text object of a fixed width, and am later checking the height to see if it’s too big.

 local messageText = display.newText( messages[i].MESSAGE, 0, 0, rowWidth, 0, native.systemFont, 32 ) -- height 0, and system sets it to actual height generated   
  
 if( messageText.height \> 100 ) then -- Is it too big\>  
 -- change font size, regenerate text... do in a loop if you want to...  
 end  

I’m actually doing something different with my code – I’m measrung the height of my text, and then building some UI elements around it (tableView rows), but I’ve got a limit on how large they each will get… But anywho, getting the height of the text is key to a lot of the formatting.
[import]uid: 79933 topic_id: 32904 reply_id: 130755[/import]

As mpappas said, you should be able to get the width and height of the text object after you create it. Then, assuming it is too big, you could:

  1. Remove the text object and create it again with a smaller font.

  2. Scale it with the obj:scale(x,y) function

With regards to scaling, scale both X and Y by the same amount, or you will goof up the aspect ratio of your text.

Example:

Target < width, height > => < 100 , 32 >
Initial < width, height > => < 116 , 46 >
Perfect scale < width, height > => < 100/116 , 32/46 > == < 0.862, 0.696 >

To maintain the aspect ratio, you would scale with the smaller of the two perfect scale values:

[lua] messageText:scale( 0.696, 0.696 )[/lua]

[import]uid: 110228 topic_id: 32904 reply_id: 130849[/import]

As mpappas said, you should be able to get the width and height of the text object after you create it. Then, assuming it is too big, you could:

  1. Remove the text object and create it again with a smaller font.

  2. Scale it with the obj:scale(x,y) function

With regards to scaling, scale both X and Y by the same amount, or you will goof up the aspect ratio of your text.

Example:

Target < width, height > => < 100 , 32 >
Initial < width, height > => < 116 , 46 >
Perfect scale < width, height > => < 100/116 , 32/46 > == < 0.862, 0.696 >

To maintain the aspect ratio, you would scale with the smaller of the two perfect scale values:

[lua] messageText:scale( 0.696, 0.696 )[/lua]

[import]uid: 110228 topic_id: 32904 reply_id: 130849[/import]