美文网首页iOS
UILabel 宽高自适应

UILabel 宽高自适应

作者: cocoawork丶 | 来源:发表于2016-11-24 14:12 被阅读15次
    //计算宽度
    - (CGFloat)calculateRowWidth:(NSString *)string {
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 30) options:NSStringDrawingUsesLineFragmentOrigin |
                       NSStringDrawingUsesFontLeading attributes:dic context:nil];
        return rect.size.width + 5;
    }
    //计算高度
    - (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize{
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
        CGRect rect = [string boundingRectWithSize:CGSizeMake(self.view.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin |
                       NSStringDrawingUsesFontLeading attributes:dic context:nil];
        return rect.size.height;
    }
    

    相关文章

      网友评论

        本文标题:UILabel 宽高自适应

        本文链接:https://www.haomeiwen.com/subject/vqvbpttx.html