Reputation: 4383
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
Upvotes: 5
Views: 7276
Reputation: 2059
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("...")
}
Upvotes: 0
Reputation: 379
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
}
Upvotes: 2
Reputation: 91
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];
}
Upvotes: 0
Reputation: 81858
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;
Upvotes: 12
Reputation: 8460
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]
Upvotes: 1
Reputation: 1752
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 !!!
Upvotes: 4