CGFloat width = self.view.bounds.size.width;
NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:@"我是有行间距的啊!\n我是有行间距的啊!"];
NSMutableAttributedString *testAttrStr = [[NSMutableAttributedString alloc] initWithString:@"测试"];
[testAttrStr addAttributes:attr range:NSMakeRange(0, testAttrStr.length)];
CGFloat oneHeight = [testAttrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
CGFloat rowHeight = [mAttrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
// 如果超出一行,则offset=0;
if(rowHeight > oneHeight){
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
[mAttrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mAttrStr.length)];
}
self.label.attributedText = [mAttrStr copy];
可以看一下我写的分类WQJTool
支持pod导入项目
网友评论