颜色的处理
NSString *content = @"披绣闼,俯雕甍,山原旷其盈视,川泽纡其骇瞩.闾阎扑地,钟鸣鼎食之家,舸舰迷津,青雀黄龙之舳.云销雨霁,彩彻区明.落霞与孤鹜齐飞,秋水共长天一色.渔舟唱晚,响穷彭蠡之滨;雁阵惊寒,声断衡阳之浦.";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, content.length)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(content.length + 2, content.length)];
// 创建一个UILabel
UILabel *aLabel = [[UILabel alloc] init];
aLabel.attributedText = attributedString;
行间距的处理
NSString *content = @"披绣闼,俯雕甍,山原旷其盈视,川泽纡其骇瞩.闾阎扑地,钟鸣鼎食之家,舸舰迷津,青雀黄龙之舳.云销雨霁,彩彻区明.落霞与孤鹜齐飞,秋水共长天一色.渔舟唱晚,响穷彭蠡之滨;雁阵惊寒,声断衡阳之浦.";
UILabel *contentLabel = [[UILabel alloc] init];
// 内容的高度
CGFloat content_height = [content sizeWithFont:[UIFont systemFontOfSize:15] withMaxSize:CGSizeMake(300, MAXFLOAT)].height;
NSNumber *count = @((content_height) / self.contentLabel.font.lineHeight);
// 内容
contentLabel.frame = // Label的frame
contentLabel.text = content;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 8; // 设置行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, contentLabel.text.length)];
contentLabel.attributedText = attributedString;
计算文字的高度方法
- (CGSize)sizeWithFont:(UIFont *)font withMaxSize:(CGSize)maxSize {
NSDictionary *attrs = @{NSFontAttributeName:font};
return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil ].size;
}