UILable高度计算

一.OC

-(int)getWithFromStr:(NSString*)str font:(float)font {
    NSDictionary *attribute = @{NSFontAttributeName: self.titleLabel.font};
    CGSize retSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 20)
                                       options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                    attributes:attribute
                                       context:nil].size;
    return retSize.width;
}

一.Swift

let title = "这里是一个好地方"
let titleFont = 16.0
let titleSize = title.size(attributes: [NSFontAttributeName: titleFont])

你可能感兴趣的:(UILable高度计算)