iOS-段间距

//行间距

/*

* space 行间距设置

* label 原始label

* string 需要输入的内容

*/

+ (UILabel *)setLineSpace:(CGFloat)space FromLabel:(UILabel *)label withString:(NSString *)string;

{

label.numberOfLines = 0;

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];

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

[paraStyle setLineSpacing:space];

[attributeString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [string length])];

label.attributedText = attributeString;

[label sizeToFit];

return label;

}

//自适应高度和高度

/*

* string 需要输入的内容

* label  原始label

* height 最大高度

* space  行间距

*/

+ (UILabel *)setAutoFrame:(NSString *)string FromLabel:(UILabel *)label withMaxHeight:(CGFloat)height byLineSpace:(CGFloat)space;

{

label.numberOfLines = 0;

label.lineBreakMode = NSLineBreakByTruncatingTail;

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];

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

[paraStyle setLineSpacing:space];

[attributeString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [string length])];

label.attributedText = attributeString;

CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, height)];

label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, size.width, size.height);

return label;

}

你可能感兴趣的:(iOS-段间距)