美文网首页
iOS如何正确设置行高,行间距

iOS如何正确设置行高,行间距

作者: zooleebee | 来源:发表于2018-09-11 16:27 被阅读20次

设置行间距为10

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineSpacing = 10 - (label.font.lineHeight - label.font.pointSize);
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

设置行高为30

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.maximumLineHeight = lineHeight;
paragraphStyle.minimumLineHeight = lineHeight;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
CGFloat baselineOffset = (lineHeight - label.font.lineHeight) / 4;
[attributes setObject:@(baselineOffset) forKey:NSBaselineOffsetAttributeName];
label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

备注

设置了行间距后,文字是单行时也会有一个行间距。所以使用设置行高的方式设置行间距更好

参考

相关文章

网友评论

      本文标题:iOS如何正确设置行高,行间距

      本文链接:https://www.haomeiwen.com/subject/wlmugftx.html