我想在 UILabel 中显示前 500 个字符,如果可用字符超过 500 个,我想显示截断图标。但我不知道如何限制 500 个字符来截断文本?
这是我的代码
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;
请告诉我,我如何显示最少 500 个字符而不是将其截断(如果超过 500 个字符)
感谢任何帮助
最佳答案
如果字符串超过 500 个字符,则将其截断。唯一需要注意的是:确保不要在 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;
关于iphone - 将 UILabel 限制为 500 个字符,然后在 IOS 中截断 UIlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15428761/