UIlabel 调整字间距

iOS UILabel设置行间距和字间距并计算高度


IOS--UILabel 用法详解


//compareString长度最长的字符串,currentString当前字符串  label要加载的label

num 当前字符串长度

+(void)setLabel:(UILabel *)label withString:(NSString *)currentString compareString:(NSString *)compareString WithFont:(UIFont *)font withNumber:(CGFloat)num

{

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

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

//paraStyle.lineSpacing = 6; //设置行间距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

CGSize  compareSize = [compareString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;

CGSize  currentSize = [currentString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;

//设置字间距 NSKernAttributeName:@1.5f

NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:[NSNumber numberWithFloat:(compareSize.width-currentSize.width)/(num-1.0f)]};

NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:currentString attributes:dic];

label.attributedText = attributeStr;

}



[common setLabel:_typeLab withString:type compareString:@"组织机构名称" WithFont:[UIFont systemFontOfSize:14] withNumber:(CGFloat)type.length];

你可能感兴趣的:(UIlabel 调整字间距)