h1z3y3/h1z3y3.github.io

根据字数自适应高度的UILabel | 东隅已逝/桑榆非晚

Opened this issue · 0 comments

https://h1z3y3.me/posts/uilabel-with-height-fixed/

  • (UILabel *) getLabelHeightFixedWithText: (NSString *) text { UIFont *font = [UIFont boldSystemFontOfSize: 12.0f]; int width = 225, height = 10000; NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init]; [attrs setObject: font forKey: NSFontAttributeName]; CGRect size = [text boundingRectWithSize:CGSizeMake(width, height) options: NSStringDrawingUsesLineFragmentOrigin attributes: attrs context: nil]; UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, size.size.width, size.size.height)]; label.numberOfLines = 0;//一定要设置行数为0 label.font = font; label.lineBreakMode = NSLineBreakByWordWrapping; label.text = (text ? text : @""); label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor blackColor]; return label; }