1. 程式人生 > >label 根據文字label自適應寬度

label 根據文字label自適應寬度

key sdi name objects extc tof 限制 alloc ict

UILabel *Label = [[UILabel alloc]init];

Label.text = @"長一點也是可以的";

Label.textColor = black_Color;

Label.textAlignment = NSTextAlignmentLeft;

gsmcLabel.font = [UIFont systemFontOfSize:12];

CGSize size = [Label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:Label.font,NSFontAttributeName,nil]];

CGFloat lblWidth = size.width;

Label.frame = CGRectMake(20, 0, lblWidth, 40);

如果長度有限制 加一個判斷

label 根據文字label自適應寬度