UILabel 字体间距和行间距,计算高度,首行缩进, 多个UILabel显示文字对齐

开发中经常有要求一段文字要有显示隔开多大的行间距,字跟字之间也要有间距,首行还要有缩进,想想也是真苛刻。那么废话不多说,下面就给大家上方法。

已封装好方法 可以加入到自己的Category

1、UILabel 设置行间距 和 字间距

UILabel 字体间距和行间距,计算高度,首行缩进, 多个UILabel显示文字对齐_第1张图片
52C5D9D2-039B-45C6-914A-CAE7465B1DF8.png
/**
 *  给UILabel设置行间距和字间距
 *  @param space 间距
 *  @param text  内容
 *  @param font  字体
 *  @param zpace  字间距 --> @10 这样设置  默认的话设置 0 就ok
 */
-(void)LW_setLineSpace:(CGFloat)space withLabelText:(NSString*)text withFont:(UIFont*)font withZspace:(NSNumber *)space
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode =NSLineBreakByCharWrapping;
    paraStyle.alignment =NSTextAlignmentLeft;
    paraStyle.lineSpacing = space; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent =0.0;
    paraStyle.paragraphSpacingBefore =0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic;
    if (zpace == 0) {
        dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f
                             };
    }else {
        dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:zpace
               };
    }
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:text attributes:dic];
    self.attributedText = attributeStr;
}
使用方法:[label1 LW_setLineSpace:5 withLabelText:@"我是字间距为5和行间距为5的字符串哦,我是字间距为5和行间距为5的字符串哦,我是字间距为5和行间距为5的字符串哦" withFont:[UIFont systemFontOfSize:17] withZspace:@5];

那么行间距和字间距设置好之后,下面来看看计算一下设置间距后的UILabel 的高度

2、UILabel 设置行间距 和 字间距 后的高度计算

/**
 *  计算UILabel的高度(带有行间距的情况)
 *  @param text  内容
 *  @param font  字体
 *  @param width 宽度
 *  @return 高度
 *  @param zpace  字间距 --> @10 这样设置  默认的话设置 0 就ok
 */
-(CGFloat)LW_getSpaceLabelHeight:(NSString*)text withFont:(UIFont*)font withWidth:(CGFloat)width withSpace:(CGFloat)space withZspace:(NSNumber *)zpace{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode =NSLineBreakByCharWrapping;
    paraStyle.alignment =NSTextAlignmentLeft;
    paraStyle.lineSpacing = space;
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent =0.0;
    paraStyle.paragraphSpacingBefore =0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic;
    if (zpace == 0) {
        dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f
               };
    }else {
        dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:zpace
               };
    }
    
    CGSize size = [text boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return size.height;
}
使用方法: CGFloat height = [label1 LW_getSpaceLabelHeight:@"我是字间距为5和行间距为5的字符串哦,我是字间距为5和行间距为5的字符串哦,我是字间距为5和行间距为5的字符串哦" withFont:[UIFont systemFontOfSize:17] withWidth:LWSCREEN_WIDTH-20 withSpace:5 withZspace:@5];

3、接下来,再看看 UILabel 的首行缩进如何 缩进并且返回高度

UILabel 字体间距和行间距,计算高度,首行缩进, 多个UILabel显示文字对齐_第2张图片
2C666B39-AE85-40C1-9918-41042694FE3C.png

PS: 如果高度计算失败,可以提前设置下frame 不然可能会高度计算失败 label 要设置换行显示

/**
 *  首行缩进 计算字符串高度
 *  @param text     内容
 *  @param indentationNum 要缩进几个字符
 *  @param alignment   对齐格式
 *  @param fontSize 字体大小
 *  @param width    宽度
 *  @return 返回高度
 */
- (CGFloat)LW_getIndentationLabelHeight:(NSString *)text withStrIndentationNum:(int)indentationNum withNSTextAlignment:(NSTextAlignment )alignment withFontSize:(CGFloat)fontSize withWidth:(CGFloat)width
{
    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];
    paraStyle01.alignment = alignment;  //对齐
    paraStyle01.headIndent = 0.0f;//行首缩进
    //参数:(字体大小17号字乘以2,34f即首行空出两个字符)
    CGFloat emptylen = fontSize * indentationNum;
    paraStyle01.firstLineHeadIndent = emptylen;//首行缩进
    paraStyle01.tailIndent = 0.0f;//行尾缩进
    paraStyle01.lineSpacing = 2.0f;//行间距
    
    NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName:paraStyle01}];
    self.attributedText = attrText;
    CGFloat height = [self sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX)].height;
    return height;
}
使用方法:CGFloat height = [label1 LW_getIndentationLabelHeight:@"首行缩进并且计算字符串高度,首行缩进并且计算字符串高度,首行缩进并且计算字符串高度,首行缩进并且计算字符串高度,首行缩进并且计算字符串高度,首行缩进并且计算字符串高度" withStrIndentationNum:2 withNSTextAlignment:NSTextAlignmentLeft withFontSize:17 withWidth:LWSCREEN_WIDTH-20];

4、UILabel文字对齐

UILabel 字体间距和行间距,计算高度,首行缩进, 多个UILabel显示文字对齐_第3张图片
B0569B40-1BCF-4C09-AE88-6E7BA07E023C.png
- (void)LW_labelTextAlignment
{
    NSMutableAttributedString *attributeString =  [[NSMutableAttributedString alloc] initWithString:self.text];
    CGSize attributeSize = [attributeString.string sizeWithAttributes:@{NSFontAttributeName:self.font}];
    CGSize adjustedSize = CGSizeMake(ceilf(attributeSize.width), ceilf(attributeSize.height));
    CGSize frame = self.frame.size;
    NSNumber *wordSpace = [NSNumber numberWithInt:((frame.width-adjustedSize.width)/(attributeString.length-1))];
    [attributeString addAttribute:NSKernAttributeName value:wordSpace range:NSMakeRange(0, attributeString.length-1)];
    self.attributedText = attributeString;
}
使用方法: [label LW_labelTextAlignment];

你可能感兴趣的:(UILabel 字体间距和行间距,计算高度,首行缩进, 多个UILabel显示文字对齐)