iOS UILabel自適應寬度
阿新 • • 發佈:2019-01-08
UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake(100, 100, 80, 20)];
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor blackColor];
label.text = @"很久很久查詢";
label.textAlignment = NSTextAlignmentCenter;
codeLabel.font = [UIFontsystemFontOfSize:12];
[self.viewaddSubview:codeLabel];
//第一種方法
CGSize size = [label.textsizeWithFont:label.fontconstrainedToSize:CGSizeMake(300, label.frame.size.height)];
//第二種方法
CGSize size = [label.textboundingRectWithSize:CGSizeMake(300, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOriginattributes