2D Toolkit Forum

2D Toolkit => Support => Topic started by: awejk on February 18, 2013, 04:57:39 pm

Title: number of lines in tk2dTextMesh
Post by: awejk on February 18, 2013, 04:57:39 pm
(http://i.imgur.com/APweiiC.png)
Code: [Select]
// calculation width and height tooltip window
Vector2 dimension = textMesh.GetMeshDimensionsForString(text);
int countLine = Mathf.CeilToInt(dimension.x / textMesh.wordWrapWidth);
Debug.Log("countLine = " + countLine + ", dimension.x = " + dimension.x);
float heightY = Mathf.Abs(dimension.y) * countLine;
tk2dSlicedSprite slicedSprite = GetComponent<tk2dSlicedSprite>();
slicedSprite.dimensions = new Vector2(slicedSprite.dimensions.x, heightY);

why the number of lines does not correspond to the actual count?
Where did I wrong?
Title: Re: number of lines in tk2dTextMesh
Post by: nikolic on February 18, 2013, 05:08:07 pm
This is not correct way to count number of lines.
Word wrapping breaks the lines between the words, thus there are many "wasted" horizontal pixels left.

Have you tried to count number of '\n' characters in already formatted string?
Title: Re: number of lines in tk2dTextMesh
Post by: unikronsoftware on February 18, 2013, 11:20:41 pm
I'll add some API functions for this in the next beta. Currently, you'll have to hack the FormatText to return the formatted string, from which you can count the number of newlines.
Title: Re: number of lines in tk2dTextMesh
Post by: awejk on February 19, 2013, 08:21:31 am
Thank you guys. Indeed the problem was '\n'. Replaced by formattedString and now works better.