UILabel的富文本属性和计算高度

注:新添加一个富文本方法,在文字只有一行的时候,取消行间距!

把下面两个方法随便放到一个公共的文件里,在.h声明这两个方法的方法名,在.m中粘如下面两个方法,需要的时候只需要导入头文件,就可以设置富文本属性,和计算富文本的高度

/**

  • 富文本
  • @param string 文本
  • @param font 字体
  • @param lineSpacing 行高
  • @param lineHeightMultiple 行间距(1.5倍)
  • @param lineBreakMode 段落样式
  • @return
    */
    + (NSMutableAttributedString *)getAttributedString:(NSString *)string font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.lineHeightMultiple = lineHeightMultiple;
    paragraphStyle.lineBreakMode = lineBreakMode;
    NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle,NSForegroundColorAttributeName:[UIColor blackColor]};
    [attributedString addAttributes:cyZoneDocAttribute range:NSMakeRange(0, [string length])];
    return attributedString;
    }

/**

  • 计算文本高度

  • @param text 文本

  • @param limitW 文本宽度

  • @param font 字体

  • @param lineSpacing 行高

  • @param lineHeightMultiple 行间距

  • @param lineBreakMode 段落样式

  • @return
    */
    + (CGSize)calculateSizeWithText:(NSString *)text limitWidth:(CGFloat)limitW font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{

      NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.lineHeightMultiple = lineHeightMultiple;
    paragraphStyle.lineBreakMode = lineBreakMode;
    
    NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};
    
    CGSize size;
    size = [text boundingRectWithSize:CGSizeMake(limitW, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:cyZoneDocAttribute context:nil].size;
    return size;
    }
    

注:对之前的方法进行修改,因为设置富文本的属性的时候可能出现只有一行文字,还出现行间距,这样会让我们的约束特别的困难,新增加一个方法,在设置富文本属性的时候多传入一个宽度,确保在只有一行文字的时候,不会有行间距,方法如下:

/**

  • 富文本

  • @param string 文本

  • @param font 字体

  • @param lineSpacing 行高

  • @param lineHeightMultiple 行间距(1.5倍)

  • @param lineBreakMode 段落样式

  • @param limitWith 文本宽度

  • @return
    */
    + (NSMutableAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode limitWith:(CGFloat)limitWith{
    if (string == nil) {
    return nil;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    CGSize selfSize = [self calculateSizeWithText:string limitWidth:limitWith font:font lineSpacing:0 lineHeightMultiple:lineHeightMultiple lineBreakMode:lineBreakMode];
    CGSize oneLineSize = [self calculateSizeWithText:@"哈哈" limitWidth:MAXFLOAT font:font lineSpacing:0 lineHeightMultiple:lineHeightMultiple lineBreakMode:lineBreakMode];

    paragraphStyle.lineSpacing = selfSize.height > oneLineSize.height ? lineSpacing : 0;
    paragraphStyle.lineHeightMultiple = lineHeightMultiple;
    paragraphStyle.lineBreakMode = lineBreakMode;
    
    return attributedString;
    }
    

注:增加一个单行不设置行间距的计算行高方法

/**

  • 计算文本高度

  • @param text 文本

  • @param limitW 文本宽度

  • @param font 字体

  • @param lineSpacing 行高

  • @param lineHeightMultiple 行间距

  • @param lineBreakMode 段落样式

  • @return
    */
    + (CGSize)calculateSizeWithText:(NSString *)text limitWidth:(CGFloat)limitW font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{

    CGSize selfSize = [self calculateSizeWithText:text limitWidth:limitW font:font lineSpacing:0 lineHeightMultiple:lineHeightMultiple lineBreakMode:lineBreakMode paragraphSpacing:0];
    CGSize oneLineSize = [self calculateSizeWithText:@"哈哈" limitWidth:MAXFLOAT font:font lineSpacing:0 lineHeightMultiple:lineHeightMultiple lineBreakMode:lineBreakMode paragraphSpacing:0];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = selfSize.height > oneLineSize.height ? lineSpacing : 0;
    paragraphStyle.lineHeightMultiple = lineHeightMultiple;
    paragraphStyle.lineBreakMode = lineBreakMode;
    
    NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};
    
    CGSize size;
    size = [text boundingRectWithSize:CGSizeMake(limitW, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:cyZoneDocAttribute context:nil].size;
    return size;
    }
    

注:这里增加一个设置富文本属性的时候增加段间距参数

/**

  • 富文本

  • @param string 文本

  • @param font 字体

  • @param lineSpacing 行高

  • @param lineHeightMultiple 行间距(1.5倍)

  • @param lineBreakMode 段落样式

  • @param paragraphSpacing 段间距(1.5倍)

  • @return
    */
    + (NSMutableAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode paragraphSpacing:(CGFloat)paragraphSpacing{
    if (string == nil) {
    return nil;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.lineHeightMultiple = lineHeightMultiple;
    paragraphStyle.lineBreakMode = lineBreakMode;
    paragraphStyle.paragraphSpacing = paragraphSpacing;

    NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};
    
    [attributedString addAttributes:cyZoneDocAttribute range:NSMakeRange(0, [string length])];
    return attributedString;
    }
    

注:这里添加增加一个有段间距的方法计算行高

/**

  • 计算文本高度

  • @param text 文本

  • @param limitW 文本宽度

  • @param font 字体

  • @param lineSpacing 行高

  • @param lineHeightMultiple 行间距

  • @param lineBreakMode 段落样式

  • @param paragraphSpacing 段间距(1.5倍)

  • @return
    */
    + (CGSize)calculateSizeWithText:(NSString *)text limitWidth:(CGFloat)limitW font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode paragraphSpacing:(CGFloat)paragraphSpacing{

      NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.lineHeightMultiple = lineHeightMultiple;
    paragraphStyle.lineBreakMode = lineBreakMode;
    paragraphStyle.paragraphSpacing = paragraphSpacing;
    
    NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};
    
    CGSize size;
    size = [text boundingRectWithSize:CGSizeMake(limitW, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:cyZoneDocAttribute context:nil].size;
    
    return size;
    }

你可能感兴趣的:(UILabel的富文本属性和计算高度)