一行代码搞定UIlabel的高度自适应和行距

给UIlabel创建分类 UILabel+LabelHeight.h

1.在.h中写接口,传给它必要的参数

/**

一行代码设置label的高度自适应 和 行间距

@param frame            label的frame (高度传入 0 或者其他)

@param contentStr      文本内容

@param font            字体

@param lineSpace        行间距

@param textlengthSpace  字间距

@param paragraphSpacing 段间距

@return 返回UILabel

*/

+(UILabel *)getLineSpaceLabelWithFrame:(CGRect )frame contentString:(NSString*)contentStr withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace  textlengthSpace:(NSNumber* )textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing;


2.在.m中实现方法

+(UILabel *)getLineSpaceLabelWithFrame:(CGRect)frame contentString:(NSString *)contentStr withFont:(UIFont *)font withLineSpace:(CGFloat)lineSpace textlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing

{

UILabel  *lab = [[UILabel alloc] init];

lab.font = font;

lab.textAlignment = NSTextAlignmentLeft;

lab.numberOfLines = 0;

NSDictionary *attributeDict  = [self setTextLineSpaceWithString:contentStr withFont:font withLineSpace:lineSpace  withTextlengthSpace:textlengthSpace paragraphSpacing:paragraphSpacing];

CGSize size = [contentStr boundingRectWithSize:frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributeDict context:nil].size;

CGFloat sizeHeight = size.height;

lab.frame =  CGRectMake(frame.origin.x,frame.origin.y, frame.size.width, sizeHeight);

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:contentStr attributes:attributeDict];

lab.attributedText = attributedString;

return lab;

}

/*

*给UILabel设置行间距和字间距

*/

+(NSDictionary *)setTextLineSpaceWithString:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace withTextlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing{

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

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = lineSpace;

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

NSDictionary *dic = @{NSFontAttributeName:font,

NSParagraphStyleAttributeName:paraStyle,

NSKernAttributeName:textlengthSpace

};

return dic;

}

3.在viewController里面调用分类中的方法

先把 UILabel+LabelHeight.h 头文件导入

NSString * conText = @"处女座的人,一般都对学识渊博的人,怀着崇敬和想亲近的心。他认为知识是很重要的,丰富的知识可以使人通达于天地之间,不会被命运中不可解的谜所困扰不前。当命运之谜棋阻在他面前时,会激起他更强烈的购物欲,并使他兴致勃勃地从各种角度去诠释它。因此,处女座的人很懂得如何去安慰一个失意的朋友。一般都对学识渊博的人,怀着崇敬和想亲近的心。他认为知识是很重要的,丰富的知识可以使人通达于天地之间,不会被命运中不可解的谜所困扰不前。当命运之谜棋阻在他面前时,会激起他更强烈的购物欲,并使他兴致勃勃地从各种角度去诠释它。因此,处女座的人很懂得如何去安慰一个失意的朋友";

UIFont * font = [UIFont systemFontOfSize:14]; //字号

CGFloat lineSpace = 8;//行间距

CGFloat paragraphSpacing = 0;//段间距

NSNumber * textLengthSpace  = @0.5;//字间距

UILabel *label = [UILabel getLineSpaceLabelWithFrame:CGRectMake(20, 150, self.view.bounds.size.width - 40, 0)  contentString:conText withFont:font withLineSpace:lineSpace  textlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];

label.backgroundColor = [UIColor yellowColor];

label.textColor = [UIColor redColor];

[self.view addSubview:label];

实现效果如下:


Demo:GitHub地址

推荐阅读更多精彩内容