通常我们使用UILabel只是显示较短的字符串,下面是一种获取字符串的换行高度,显示
代码如下所示:
[cpp] view plain copy print ?
NSString* str = @"test testImplement viewDidLoad to do additional setup after loading the view, typically from a nib";
//CGSize labelSize = [str sizeWithFont:[UIFont systemFontOfSize:14]];
//可以精确计算字符串的换行,高宽等
CGSize labelSize = [str sizeWithFont:[UIFont boldSystemFontOfSize:17.0f]
constrainedToSize:CGSizeMake(280, 100)
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(50, 20, labelSize.width, labelSize.height);
label.backgroundColor = [UIColor whiteColor];
label.text = str;
label.font = [UIFont systemFontOfSize:17.0f];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeCharacterWrap;
[self.view addSubview:label];
[label release];