iOS-UILabel的宽度自适应文字


UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size
[label setNumberOfLines:0];
NSString *s = @"abcdefghijklmn";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];

你可能感兴趣的:(ios)