Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit the UILabel to 500 character and than Truncate the UIlabel in IOS

I want to display first 500 character in UILabel and than display Truncate icon if there is more than 500 character available.But i dont know how can i limit 500 character to truncate the text?.

Here is my code

label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.bounds.size.width, 30)];
   // In this case value of self.bounds.size.width is "427"

    label2.backgroundColor = [UIColor clearColor];
    label2.numberOfLines = 2;
    label2.textAlignment = UITextAlignmentCenter;
    label2.font = [UIFont systemFontOfSize:13];
    [self addSubview:label2]

         //Here is Implimentation code of my label

 NSString *temp = [galleryEntryTree objectForKey:@"description"];// calling lebel text from database
coverView.label2.text = temp;
coverView.label2.adjustsFontSizeToFitWidth = NO;
coverView.label2.lineBreakMode = UILineBreakModeTailTruncation;

Just tell me guys how can i display min 500 character and than truncate it( if longer than 500)

Any help is appreciated

like image 830
Swap-IOS-Android Avatar asked Mar 15 '13 09:03

Swap-IOS-Android


1 Answers

Just truncate the string if it's longer than 500 characters. Only caveat: make sure to not break it in the middle of a surrogate pair:

NSString *temp = [galleryEntryTree objectForKey:@"description"];
if ([temp length] > 500) {
    NSRange range = [temp rangeOfComposedCharacterSequencesForRange:(NSRange){0, 500}];
    temp = [temp substringWithRange:range];
    temp = [temp stringByAppendingString:@" …"];
}
coverView.label2.text = temp;
like image 78
Nikolai Ruhe Avatar answered Nov 14 '22 15:11

Nikolai Ruhe