标签:
label自适应:
- (void)viewDidLoad {
[super viewDidLoad];
_label.numberOfLines = 0; //必须将显示的行数设置为无限制
_label.font = [UIFont systemFontOfSize:17];
_label.textAlignment = NSTextAlignmentLeft;
_label.text = @" ds fak dbsafwibeeuifbbdsabagsdbfuiasefiuwBDHBSAF";//随意设置label文本
CGSize size = [self sizeWithString:_label.text font:_label.font]; //调用封装方法
_label.bounds = CGRectMake(0, 0, size.width, size.height);
_label.center = self.view.center;
}
//自适应size设置
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
CGRect rect = [string boundingRectWithSize:CGSizeMake(200, 300) //限制最大的宽度和高度
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin //采用换行模式
attributes:@{NSFontAttributeName:font} //传入字体
context:nil];
return rect.size;
}
//原文:http://www.2cto.com/kf/201411/353171.html
标签:
原文地址:http://www.cnblogs.com/ganjinjin/p/4946180.html