通过UILabel 计算文本的宽度
UILabel *label = [[UILabel alloc] init];
label.text = @"searchKey";
label.font = [UIFont systemFontOfSize:14.0];
[label sizeToFit];
CGFloat width = ceil(label.frame.size.width) + 1;
通过UILabel 计算文本的高度
UILabel *label = [[UILabel alloc] init];
label.text = @"searchKey";
label.font = [UIFont systemFontOfSize:14.0];
CGSize labelSize = [_textInfoLabel sizeThatFits:CGSizeMake(230.f, MAXFLOAT)];//指定宽度,来确定高度
[label sizeToFit];
CGFloat width = ceil(label.frame.size.width) + 1;
通过字符串NSString计算文本的高度和宽度
NSString *searchKey = @"searchKey";
CGSize size = [searchKey sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.]}];
CGFloat width = ceil(size.width);
CGFloat height = ceil(size.height);