根据字体计算CGRect

    UILabel *label = [[UILabel alloc]init];
    label.numberOfLines = 0;//多行显示
    label.backgroundColor = [UIColor yellowColor];
    label.font = [UIFont systemFontOfSize:20];
    NSString *string = @"我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国我爱中国";
    UIFont *font = [UIFont systemFontOfSize:20];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 20;//行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:font,
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };//其他属性可以在UIKit的第一个头文件中查看,颜色。。
    CGRect size = [string boundingRectWithSize:CGSizeMake(200, 10000)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:attributes
                                       context:nil];//iOS 7.0有效
    NSAttributedString *attributeString =  [[NSAttributedString alloc]initWithString:string attributes:attributes];//设置属性字体

    label.frame = size;
    label.attributedText = attributeString;
    label.center = self.view.center;
    [self.view addSubview:label];

封装的方法:

- (CGSize)textSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
{
    CGSize textSize;
    if ([XWCHelper isIOS7orHigher]) {
        
        if (CGSizeEqualToSize(size, CGSizeZero)) {
            
            textSize =  [self sizeWithAttributes:@{NSFontAttributeName:font}];
        }
        else{
            
            NSStringDrawingOptions option = NSStringDrawingUsesLineFragmentOrigin;
            //NSStringDrawingTruncatesLastVisibleLine如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。 如果指定了NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略 NSStringDrawingUsesFontLeading计算行高时使用行间距。(译者注:字体大小+行间距=行高)
            NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
            CGRect rect = [self boundingRectWithSize:size//如果是单行的话,可以用CGSizeZero,如果高发生变化,可以使用CGSizeMake(120,CGFloat_MAX),制定一个无限大的数,让系统根据字体的大小和行距自己识别出高度
                                             options:option
                                          attributes:attributes
                                             context:nil];
            textSize = rect.size;
        }
        
    }
    else{
        
        if (CGSizeEqualToSize(size, CGSizeZero)) {
            
            textSize =[self sizeWithFont:font];
        }
        else{
            
            textSize =[self  sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
        }
    }
    return textSize;
}

实现如下效果:

_friendDetailLabel = [UILabel labelWithFrame:CGRectMake(30 + 6 + 5, _inviteFriendBtn.bottom + 15, 0, 0) text:@"" textColor:RGBColor(214,156,3) textAlignment:NSTextAlignmentLeft font:Arial(14)];
_friendDetailLabel.numberOfLines = 0;
NSString *str = @"好友使用你的推荐码注册,TA可获得5元红包!你可获得2元推广奖励!";
//设置属性
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 7;//行间距
NSDictionary *attributes = @{
                             NSFontAttributeName:Arial(14),
                             NSParagraphStyleAttributeName:paragraphStyle
                             };
//设置可变的副文本为了修改属性
NSMutableAttributedString *attributeString =  [[NSMutableAttributedString alloc]initWithString:str attributes:attributes];//设置属性字体
//设置修改属性
NSDictionary *attributes1 = @{
                              NSFontAttributeName:Arial(18)
                              };
//设置属性
[attributeString setAttributes:attributes1 range:NSMakeRange(17, 2)];
[attributeString setAttributes:attributes1 range:NSMakeRange(26, 2)];
//计算大小
CGRect size =[attributeString boundingRectWithSize:CGSizeMake(kScreenWidth - 30 - 41, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
_friendDetailLabel.width = size.size.width;
_friendDetailLabel.height = size.size.height;
_friendDetailLabel.attributedText = attributeString;

原文地址:https://www.cnblogs.com/yyzanll/p/4662390.html