iphone - 将 UILabel 限制为 500 个字符,然后在 IOS 中截断 UIlabel

标签 iphone ios ios5 uilabel

我想在 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/

相关文章:

ios - UItableViewCell 大小不合适

iphone - 应用程序运行时推送通知不起作用(事件)

iphone - 在单独的文件中定义委托(delegate)函数(而不是在 ViewController 中)

iphone - 如何在项目中实现UIPageController

iphone - 从 sqlite 检索数据的问题

iphone - Xcode iOS部署目标检查?

iphone - 目标和项目有什么区别?

ios - 如何根据我的json中的bool值过滤和显示 TableView 和 Collection View 值

ios - 将私有(private) pod 添加到 podfile 作为依赖项

ios - 使用 iphone sdk 在应用内购买货币交易