调整label的行间距、字间距

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整label的行间距、字间距相关的知识,希望对你有一定的参考价值。

参考技术A //改变label的行间距

- (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space

NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:label.text];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[paragraphStyle setLineSpacing:space];

[attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];

label.attributedText = attributedStr;

[label sizeToFit];



//改变label的字间距

- (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@NSKernAttributeName:@(space)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];

label.attributedText = attributedString;

[label sizeToFit];



//改变行间距和字间距

- (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@NSKernAttributeName:@(wordSpace)];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[paragraphStyle setLineSpacing:lineSpace];

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];

label.attributedText = attributedString;

[label sizeToFit];

以上是关于调整label的行间距、字间距的主要内容,如果未能解决你的问题,请参考以下文章

android中怎么调整字体的间距和行间距

如何把keil中的代码行间距调宽

NSMutableAttributedString怎么设置label的行间距

iOS--UILabel设置行距和字间距,并根据文本计算高度

怎么调整行间距

c# 调整gridview行间距,如图,