Limit UILabel to 500 characters and then crop UIlabel in iOS

I want to display the first 500 characters in a UILabel and display the Truncate icon if more than 500 characters are available. But I don’t know how to limit 500 characters 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 I can display at least 500 characters and crop it (if longer than 500)

Any help is appreciated.

+5
source share
6 answers

Just trim the string if it contains more than 500 characters. Just beware: do not tear 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;
+12
source

To display only 500 characters, use only the code below:

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

Hope this helps you.

All the best !!!

+4

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
}
+2

, .

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]
+1

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

500 500 , , , .

NSString * string = LabelNewText.text;

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

Swift 3.0

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("...")
        }
0

All Articles