调整label的行间距、字间距

//改变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的行间距、字间距)