码迷,mamicode.com
首页 > 其他好文 > 详细

使用boundingRectWithSize计算内容高度的坑

时间:2015-07-31 13:12:37      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:ios7   ios   boundingrectwithsize   sizewithfont   

iOS中,根据给定的内容、字体,宽度,计算文本高度的函数,iOS7之前使用sizeWithFont,iOS7之后使用boundingRectWithSize。</span>
- boundingRectWithSize:options:attributes:context:
Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.


Discussion
To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.

This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function.

This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) are allowed to overlap the layout constraints specified by the size passed in, so in some cases the width value of the size component of the returned CGRect can exceed the width value of the size parameter.

根据Discussion描述,返回值CGRect的Size含有小数点,如果使用函数返回值CGRect的Size来定义View大小,必需使用“ceil”函数获取长宽(ceil:大于当前值的最小正数)。


先贴一段自己写的,使用代码:

传入文本内容、字体(这里使用屏幕宽度)

返回矩形区域,注意返回的CGSize长宽必需使用ceil处理

+ (CGSize)sizeForNoticeTitle:(NSString*)text font:(UIFont*)font{
    CGRect screen = [UIScreen mainScreen].bounds;
    CGFloat maxWidth = screen.size.width;
    CGSize maxSize = CGSizeMake(maxWidth, CGFLOAT_MAX);
    
    CGSize textSize = CGSizeZero;
    // iOS7以后使用boundingRectWithSize,之前使用sizeWithFont
    if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        // 多行必需使用NSStringDrawingUsesLineFragmentOrigin,网上有人说不是用NSStringDrawingUsesFontLeading计算结果不对
        NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin |
        NSStringDrawingUsesFontLeading;
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        [style setLineBreakMode:NSLineBreakByCharWrapping];
        
        NSDictionary *attributes = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName : style };
        
        CGRect rect = [text boundingRectWithSize:maxSize
                                         options:opts
                                      attributes:attributes
                                         context:nil];
        textSize = rect.size;
    }
    else{
        textSize = [text sizeWithFont:font constrainedToSize:maxSize lineBreakMode:NSLineBreakByCharWrapping];
    }
    
    return textSize;
}


这还不是我们今天讨论的坑。

坑在这!坑在这!坑在这!(重说三)

boundingRectWithSize: 方法只是取得字符串的size, 如果字符串中包含\n\r 这样的字符,也只会把它当成字符来计算。但放到UITextView中来解析时,会把它变成回车换行符,那么在显示时就会多出一行的高度出来。


而且,使用stringWithFormat才会忽略\n,使用@“”形式不会。


矬点的做法,大体的实际高度 = boundingRectWithSize计算出来的高度 + \n\r出现的次数 * 单行文本的高度


版权声明:本文为博主原创文章,未经博主允许不得转载。

使用boundingRectWithSize计算内容高度的坑

标签:ios7   ios   boundingrectwithsize   sizewithfont   

原文地址:http://blog.csdn.net/xjkstar/article/details/47165983

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!