简体   繁体   中英

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

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;

To display only 500 character just use below code:

NSString *string = YOUR_TEXT;
if ([string length] >500) {
    string = [string substringToIndex:500];
}

Hope this will help you.

All the best !!!

Here is how to do it in Swift 2.2:

let maxLength = 500
if originalString.characters.count > maxLength {
   let range = originalString.rangeOfComposedCharacterSequencesForRange(Range<String.Index>(originalString.startIndex ..< originalString.startIndex.advancedBy(maxLength)))
   let tmpValue = originalString.substringWithRange(range).stringByAppendingString(" …")
   // use tmpValue
}

try this one it'l helps you.

label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, self.bounds.size.width, 30)];
   // In this case value of self.bounds.size.width is "427"
 label2.text=@"your text................................";
 if([label2.text length]>500)
        label2.text=[label2.text substringToIndex:500];


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

It can be simply done by

NSString *LabelNewText = [YourLabelName.text substringToIndex:500];

Here 500 or less than 500 characters will be displayed and all the characters exceeding that will be truncated.

NSString *string = LabelNewText.text;

if ([string length] >500)
{
   string = [string substringToIndex:500];                                       
}

Swift 3.0 version

let maxLength = 300 //char length
if originalString.characters.count > maxLength {
            let range =  originalString.rangeOfComposedCharacterSequences(for: originalString.startIndex..<originalString.index(originalString.startIndex, offsetBy: maxLength))
            let tmpValue = originalString.substring(with: range).appending("...")
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM