iOS 讓Label根據其上的文字屬性自適應顯示

weixin_34007291發表於2016-08-30

- (void)viewDidLoad {

[super viewDidLoad];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];

label.text = @"fewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdffewfvdf";

label.numberOfLines = 0;

label.lineBreakMode = NSLineBreakByWordWrapping;

CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)];

label.frame =CGRectMake(10, 100, 300, size.height);

label.backgroundColor = [UIColor redColor];

[self.view addSubview:label];

}

相關文章