对UILabel文本的各种处理方法总结

设置文本行间距:

NSString * string = @"这里是对UILabel文本的各种处理方法总结";
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]initWithString:string];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:18];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
[textLabel setAttributedText:attributedString];

同label内文本显示不同的字体或颜色:

NSString * string = @"这里是对UILabel文本的各种处理方法总结";
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:string];
NSRange range = [string rangeOfString:@"总结";
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:range]; // 字体
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] rang:rang];//颜色
self.detailLabel.attributedText = str;

获取字符串长度

NSString * buttonStr = @"这里是对UILabel文本的各种处理方法总结";
CGSize labelSize = [buttonStr boundingRectWithSize:CGSizeMake(400, 4000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0]} context:nil].size;

你可能感兴趣的:(对UILabel文本的各种处理方法总结)