Author Topic: Clamped UILabel with three dots at the end  (Read 4629 times)

bdominguez

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Clamped UILabel with three dots at the end
« on: June 30, 2014, 05:53:07 AM »
I have a clamped label where I want to append "..." at the end if it doesn't fit.

How can I achieve that? Because now I don't know how to know if certain text fits in the area of the label and also how to append "..." characters without clamping them too.

"Lorem ipsum dolor sit amet, consectetur adipiscing elit"

To this:

"Lorem ipsum dolor sit amet..."

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Clamped UILabel with three dots at the end
« Reply #1 on: July 01, 2014, 04:00:10 AM »
This isn't a simple task.

First, call label.UpdateNGUIText() so that NGUIText's values are set correctly using the label's values.

Next you will want to determine the length of "...". You can use NGUIText.CalculatePrintedSize for that.

Once you know that, and the length of the field (say 200 pixels), subtract the length of the dots from it, giving you the maximum allowed size. Set NGUIText.rectWidth to this value and set NGUIText.maxLines to 1.

Now use NGUIText.WrapText to "wrap" your desired text. This will use the "rectWidth" you set in the previous step, giving you the text that actually fits before the "...". Lastly, set the label's text to this wrapped value plus your 3 dots.

col000r

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 3
  • Posts: 43
  • Mighty Emperor of Planet "Home Office"
    • View Profile
    • BLACKISH
Re: Clamped UILabel with three dots at the end
« Reply #2 on: October 22, 2014, 07:51:07 AM »
Hey, I'm trying to do exactly that, but can't seem to get it to work. Could you please have a look and point me in the right direction?

  1. label.UpdateNGUIText();
  2. Vector2 w = NGUIText.CalculatePrintedSize( "..." );
  3. NGUIText.rectWidth = label.width - Mathf.CeilToInt( w.x );
  4. NGUIText.maxLines = 1;
  5. string wrappedText = string.Empty;
  6. NGUIText.WrapText( text, out wrappedText );
  7. if( text != wrappedText ) label.text = wrappedText + "...";
  8. Debug.Log ( w + ", " + text + " >> " + wrappedText );
Games: BLACKISH | Blog | Assets

wongkh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Clamped UILabel with three dots at the end
« Reply #3 on: October 23, 2014, 05:09:19 AM »
you can use this....
Create new script..
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class resizeScript : MonoBehaviour {
  5.  
  6.         public UILabel my_label;
  7.         public GameObject another_label;
  8.  
  9.         // Use this for initialization
  10.         void Start () {
  11.                 //Debug.Log(my_label.width);
  12.         }
  13.        
  14.         // Update is called once per frame
  15.         void Update () {
  16.  
  17.                 //Debug.Log(my_label.CalculateOffsetToFit(my_label.text));
  18.  
  19.                 //if(my_label.width > NGUIText.CalculatePrintedSize(my_label.text).x )
  20.                 if(my_label.CalculateOffsetToFit(my_label.text) <= 0)
  21.                 {
  22.                         NGUITools.SetActive(another_label,false);
  23.                 }else{
  24.                         NGUITools.SetActive(another_label,true);
  25.                 }
  26.         }
  27. }
  28.  

then make a UILabel B with ... and anchor beside another UILabel A...
in your UILabel A, Add Component (the script file)
pull the object on it..
Script : the script name you use for the code (the code I given)
My_Label : UILabel A
Another_Label : UILabel B (the dot)

Set your UILabel A - Overflow : ClampContent
And done...Hope it help you~
 ;) ;)

col000r

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 3
  • Posts: 43
  • Mighty Emperor of Planet "Home Office"
    • View Profile
    • BLACKISH
Re: Clamped UILabel with three dots at the end
« Reply #4 on: October 23, 2014, 06:50:41 AM »
Thanks! That would have worked as well. I ended up doing this:

  1. string txt = this.label.text;
  2. this.label.UpdateNGUIText();
  3. int calcOffset = NGUIText.CalculateOffsetToFit( txt );
  4. if( calcOffset > 0 ) {
  5.         this.label.text = txt.Substring( 0, txt.Length - Mathf.Clamp( calcOffset + 2, 0, txt.Length ) ) + "...";
  6. }
Games: BLACKISH | Blog | Assets