user198725878
user198725878

Reputation: 6386

UILabel - Display ... when text length crosses certain length

I have placed UILabel in my application, in that I want to display the text with .... once the length of the text exceeds the certain count.

Because if the text goes longer, it gives the design issue.

Please let know which function to use.

Upvotes: 1

Views: 2180

Answers (7)

Muhammed Ayaz
Muhammed Ayaz

Reputation: 1398

Try this will helpful for you.

NSString *string=YourString;

int size=[YourString length];

if (size>21) 
{
    NSMutableString *string1 = [[NSMutableString alloc]init];
    char c;
    for(int index = 0;index <20 ;index++)
    {
        c =[string characterAtIndex:index];

        [string1 appendFormat:@"%c",c];
    }
    [string1 appendFormat:@"..."];
    string=string1;

}

Add "string" on your UILable.

Upvotes: 2

Hemang
Hemang

Reputation: 27050

#define EXCEEDED_LENGTH 8

- (NSString *) checkStringLength:(NSString *)str 
{
    if(str.length >= EXCEEDED_LENGTH) 
    {
       return [NSString stringWithFormat:@"%@...",[str subStringToIndex:EXCEEDED_LENGTH-1]];
    }
    return str;
}

yourLabel.text = [self checkStringLength:@"Hello World !!"];

Output like Hello Wo... For better output you can trim whitespaces before pass string to function.

Upvotes: 1

Neo
Neo

Reputation: 2807

Try this

yourLabel.lineBreakMode=UILineBreakModeTailTruncation;

If you are adding your UILabel from interface builder you can do it directly. Select you UILabel and in the Utilities column in Attriubtes Inspector=> Label section=> Line Breaks set Truncate Tail

Upvotes: 5

Jorge
Jorge

Reputation: 2066

This is a method for a Category of UILabel

-(void)setTruncatedTextWithDotsIfNeeded:(NSString *)text
    {
        float fullTextWidth = [text sizeWithFont:self.font].width;
        float labelWidth = self.frame.size.width;
        if(fullTextWidth<=labelWidth){
            [self setText:text];
            return;
        }
        NSString *dots = @"…";
        float dotsWidth = [dots sizeWithFont:self.font].width;
        NSRange fullRange = [text rangeOfString:text];
        for(int i = fullRange.length; i >= fullRange.location; i--){
            NSRange currentRange;
            currentRange.location = 0;
            currentRange.length = i;
            NSString *partialText = [text substringWithRange:currentRange];
            float partialTextWidth = [partialText sizeWithFont:self.font].width;
            if(partialTextWidth + dotsWidth <= labelWidth){
                [self setText:[NSString stringWithFormat:@"%@...",partialText]];
                return;
            }
        }
    }

Upvotes: 0

Manu
Manu

Reputation: 4750

CGSize constraint = CGSizeMake(690.0, 2000.0);
CGSize size_txt_overview1 = [[headItemArray objectAtIndex:k] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:18] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
UILabel *lbl_headitem = [[UILabel alloc]initWithFrame:CGRectMake(3,h, 690, size_txt_overview1.height)];
lbl_headitem.numberOfLines=0;

Please set your UILabel width and height in CGSize constraint. Best way.. and it worked for me.

Upvotes: 0

Kamleshwar
Kamleshwar

Reputation: 2997

Either you can make UILable size(length/width) Dynamic,

Or

You can UITextView with edit disable so if there will be long text it will be scrollable.

Upvotes: 0

Lithu T.V
Lithu T.V

Reputation: 20021

From the information you shared I think the autoshrink and linebreakermode may be the root cause.IT is the property which tries to show the contents in the specified frame which will decrease and adjust the font size

2 ways to sove the issue

  • adjust the property according to requirement
  • Increase the framesize of label(programmatically by finding size)
  • also look on the edge insets

Upvotes: 0

Related Questions