【知识总结】boundingrectwithsize方法

http://blog.csdn.net/jymn_chen/article/details/10949279

    static CGFloat font = 20;    

    NSString *string = @"即可将点击计算的发牢骚地方亮色简单斐林试剂的礼服及所经历的说的发送发送到发送到是否撒旦法说的发送是是教室里老师代理费说两句老司机福利局老师代理费说两句老司机福利局受得了浮生将点击计算的发牢骚地方亮色简单斐林试剂的礼服及所经历的说的发送发送到发送到是否撒旦法说的发送是是教室里老师代理费说两句老司机福利局受得了浮生六记的理发师老地方老师都累死了都法律手段法律手段里番精力使得分类是单例烦死了发牢骚发牢骚登陆发牢骚理发师累死了发牢骚 发送";
    
    /**
        同 5 的结果一样
     */
    CGRect frame1 = [string boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    NSLog(@"frame1 = %@", NSStringFromCGRect(frame1));
    
    
    /**
        同 3 的结果一样
     */
    CGRect frame2 = [string boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    NSLog(@"frame2 = %@", NSStringFromCGRect(frame2));
    
    /**
        单行显示的实际宽高(不包含上下行之间的间距) -- 同 4 比较
     这个宽高不受到 size 的影响,返回的是文本真实的宽和高,如:size = CGSizeMake(300, 10),返回的
     frame = {{0.91000000000000003, -1.498}, {5885.3547812499928, 13.061999999999999}}
     
     还有一点要注意:这里计算的高度,不包含上下的间距
     */
    CGRect frame3 = [string boundingRectWithSize:CGSizeMake(300, 10) options:NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    NSLog(@"frame3 = %@", NSStringFromCGRect(frame3));
    
    /**  
        1,单行显示的实际宽高(包含上下行之间的间距)-- 同 3 比较
        2,返回的宽度是 size 的宽度
     */
    CGRect frame4 = [string boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    NSLog(@"frame4 = %@", NSStringFromCGRect(frame4));
    
    
    /**  5
        1,计算得到的是文本按 size 计算之后得到的大小
        2,有包含行之间的距离
        3,返回的宽度,是实际显示所占用的宽度
        4,在 `NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading` 
的基础上继续扩充,效果也是一样的
     */
    CGRect frame5 = [string boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    NSLog(@"frame5 = %@", NSStringFromCGRect(frame5));

    CGRect frame6 = [string boundingRectWithSize:CGSizeMake(300, 1000)
                                         options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    NSLog(@"frame6 = %@", NSStringFromCGRect(frame6));

    
    CGRect frame7 = [string boundingRectWithSize:CGSizeMake(300, 1000)
                                         options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];

通过打印可以看出,NSStringDrawingUsesLineFragmentOrigin 可以获取到文本的实际显示大小,不管是否并上其他类型

1,获取文本的实际大小NSStringDrawingUsesLineFragmentOrigin
2,获取单行的高度,不包含行间距NSStringDrawingUsesDeviceMetrics
3,获取单行的高度,包含行间 NSStringDrawingTruncatesLastVisibleLineNSStringDrawingUsesFontLeading

打印数据

frame1 = {{0, 0}, {285.31999999999999, 381.875}}**
frame2 = {{0, -4.82421875}, {300, 23.8671875}}**
frame3 = {{1.3, -2.1400000000000001}, {4404.6664843750004, 18.66}}**
frame4 = {{0, -4.82421875}, {300, 23.8671875}}**
frame5 = {{0, 0}, {285.31999999999999, 381.875}}**
frame6 = {{0, 0}, {285.31999999999999, 381.875}}**
frame7 = {{0, 0}, {285.31999999999999, 381.875}}**

你可能感兴趣的:(【知识总结】boundingrectwithsize方法)