Hello Guest

Author Topic: number of lines in tk2dTextMesh  (Read 4607 times)

awejk

  • Newbie
  • *
  • Posts: 10
    • View Profile
number of lines in tk2dTextMesh
« on: February 18, 2013, 04:57:39 pm »

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?

nikolic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: number of lines in tk2dTextMesh
« Reply #1 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: number of lines in tk2dTextMesh
« Reply #2 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.

awejk

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: number of lines in tk2dTextMesh
« Reply #3 on: February 19, 2013, 08:21:31 am »
Thank you guys. Indeed the problem was '\n'. Replaced by formattedString and now works better.