计算某种字体类型的某种大小的字符串的所占用屏幕的宽高
有2种方式
方式1,通过NSString的sizeWithAttributes方法
UIFont *strFontStyleAndSizeFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
NSString *str = @"任意字符。。";
CGSize strSize = [str sizeWithAttributes:@{
NSFontAttributeName : strFontStyleAndSizeFont
}];
NSLog(@"str = %@, 占用屏幕的宽度 = %lf", str, strSize.width);
//注意:你如果直接把strSize赋值给UILabel的frame.size。UILabel会显示不全这些文字(这里只str字符串),因为UILabel默认的字体大小是17,所以你只需要把UILabel的fontSize也设置为16,就可以显示这些文字了
方式2,通过NSMutableAttributedString(和方式3本质相同,所以推荐使用方式3)
UIFont *strFontStyleAndSizeFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"任意字符"];
[attributedStr addAttribute:NSFontAttributeName value:strFontStyleAndSizeFont range:NSMakeRange(0, attributedStr.length)];
CGRect strRect = [attributedStr boundingRectWithSize:CGSizeMake(MAXFLOAT, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
NSLog(@"str = %@, 占用屏幕的宽度 = %lf", attributedStr.string, strRect.size.width);
方式3,通过NSString
NSDictionary *textAttributeDicr = @{
NSFontAttributeName : [UIFont fontWithName:@"PingFangSC-Regular" size:16]
};
NSString *str = @"fjewoijdijewijdiewjdpoewopekwpdkpeowkdopekwp";
CGRect strRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributeDicr context:nil]; //情况1:宽高都不限制,这种情况是高度基本不变,而宽度随着文字的增多而增大
// CGRect strRect = [str boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributeDicr context:nil]; //情况2:宽度最大为100,这种情况是宽度最大是100,而高度随着文字的增多而增大!
demo(宽度固定,高度随文字的增多而增大)
结果如下图:
代码如下:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
UILabel *label = [[UILabel alloc] init];
UIFont *strFontStyleAndSizeFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"fjewoijdijewijdiewjdpoewopekwpdkpeowkdopekwp"];
[attributedStr addAttribute:NSFontAttributeName value:strFontStyleAndSizeFont range:NSMakeRange(0, attributedStr.length)];
// CGRect strRect = [attributedStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil]; //情况1:宽高都不限制,这种情况是高度基本不变,而宽度随着文字的增多而增大
CGRect strRect = [attributedStr boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil]; //情况2:宽度最大为100,这种情况是宽度最大是100,而高度随着文字的增多而增大!
NSLog(@"str = %@, 占用屏幕的宽度 = %lf, 高度 = %lf", attributedStr.string, strRect.size.width, strRect.size.height);
label.backgroundColor = UIColor.redColor;
label.text = attributedStr.string;
label.numberOfLines = 0;
label.frame = CGRectMake(0, 100, strRect.size.width, strRect.size.height);
label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16]; //因为UILabel的默认字体大小是17,所以要改。
[self.view addSubview:label];
}
@end
demo2(高度”固定“,宽度随文字的增多而增大)
结果如下图:
代码如下:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
UILabel *label = [[UILabel alloc] init];
UIFont *strFontStyleAndSizeFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"fjewoijdijewijdiewjdpoewopekwpdkpeowkdopekwp"];
[attributedStr addAttribute:NSFontAttributeName value:strFontStyleAndSizeFont range:NSMakeRange(0, attributedStr.length)];
CGRect strRect = [attributedStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil]; //情况1:宽高都不限制,这种情况是高度基本不变,而宽度随着文字的增多而增大
// CGRect strRect = [attributedStr boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil]; //情况2:宽度最大为100,这种情况是宽度最大是100,而高度随着文字的增多而增大!
NSLog(@"str = %@, 占用屏幕的宽度 = %lf, 高度 = %lf", attributedStr.string, strRect.size.width, strRect.size.height);
label.backgroundColor = UIColor.redColor;
label.text = attributedStr.string;
label.numberOfLines = 0;
label.frame = CGRectMake(0, 100, strRect.size.width, strRect.size.height);
label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16]; //因为UILabel的默认字体大小是17,所以要改。
[self.view addSubview:label];
}
@end