ios: 计算某种字体类型的某种大小的字符串的所占用屏幕的宽高

有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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值