iOS获取label的高度<模仿博友>

在iOS开发中,想要获取label的高度是不言而喻的!

之前我还以为,这是不可能的事!

 

//在控制器中加一个label,代码如下:

- (void)viewDidLoad {

    [super viewDidLoad];

    // label的文本

    NSString *strText = @"如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.如果真的有一天,爱情理想会突变.";

    // 创建label

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(30, 55, 300, 0)];//height为0,就这么任性

    label.font = [UIFont systemFontOfSize:15];

    label.numberOfLines = 0;

    label.text = strText;

    label.backgroundColor = [UIColor purpleColor];

    // 显示label

    [self.view addSubview:label];

    // 开始获取高度

    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:strText];

    // 设置的字体

    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, strText.length)];

    //这里是一个NSString的分类方法

    CGSize size = [strText sizeWithAttributeStr:attrString withConstraints:CGSizeMake(300, MAXFLOAT) numberOfLines:0];

    //重新设置label的frame

    CGRect frame = label.frame;

    frame.size.height = size.height;

    label.frame = frame;

}

 

分类的实现:

#import "NSString+ZHHExtension.h"

#import <CoreText/CoreText.h>

 

static CGFloat const TTTFLOAT_MAX = 100000;

 

static inline CGFLOAT_TYPE CGFloat_ceil(CGFLOAT_TYPE cgfloat) {

#if CGFLOAT_IS_DOUBLE

    return ceil(cgfloat);

#else

    return ceilf(cgfloat);

#endif

}

 

 

static inline CGSize CTFramesetterSuggestFrameSizeForAttributedStringWithConstraints(CTFramesetterRef framesetter, NSAttributedString *attributedString, CGSize size, NSUInteger numberOfLines) {

    CFRange rangeToSize = CFRangeMake(0, (CFIndex)[attributedString length]);

    CGSize constraints = CGSizeMake(size.width, TTTFLOAT_MAX);

    

    if (numberOfLines == 1) {

        // If there is one line, the size that fits is the full width of the line

        constraints = CGSizeMake(TTTFLOAT_MAX, TTTFLOAT_MAX);

    } else if (numberOfLines > 0) {

        // If the line count of the label more than 1, limit the range to size to the number of lines that have been set

        CGMutablePathRef path = CGPathCreateMutable();

        CGPathAddRect(path, NULL, CGRectMake(0.0f, 0.0f, constraints.width, TTTFLOAT_MAX));

        CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

        CFArrayRef lines = CTFrameGetLines(frame);

        

        if (CFArrayGetCount(lines) > 0) {

            NSInteger lastVisibleLineIndex = MIN((CFIndex)numberOfLines, CFArrayGetCount(lines)) - 1;

            CTLineRef lastVisibleLine = CFArrayGetValueAtIndex(lines, lastVisibleLineIndex);

            

            CFRange rangeToLayout = CTLineGetStringRange(lastVisibleLine);

            rangeToSize = CFRangeMake(0, rangeToLayout.location + rangeToLayout.length);

        }

        

        CFRelease(frame);

        CFRelease(path);

    }

    

    CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, rangeToSize, NULL, constraints, NULL);

    

    return CGSizeMake(CGFloat_ceil(suggestedSize.width), CGFloat_ceil(suggestedSize.height));

}

 

@implementation NSString (ZHHExtension)

 

//为了获取某个字符串在某种情况下的size

- (CGSize) sizeWithAttributeStr:(NSAttributedString *)attributedString withConstraints:(CGSize)size

                  numberOfLines:(NSUInteger)numberOfLines

{

    if (!attributedString || attributedString.length == 0) {

        return CGSizeZero;

    }

    

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString);

//    CTFramesetterSuggestFrameSizeForAttributedStringWithConstraints

    CGSize calculatedSize =CTFramesetterSuggestFrameSizeForAttributedStringWithConstraints(framesetter, attributedString, size, numberOfLines);

    

    CFRelease(framesetter);

    

    return calculatedSize;

}

@end

 

最终效果:

很不错的一个方法.

 

我也是模仿别人的代码,只是自己写成一个分类,模仿了一下.具体详情:参考博客:http://www.cnblogs.com/wb145230/p/4464430.html,也在此感谢此博友的分享!

posted @ 2015-04-29 09:28  花园晓雨  阅读(436)  评论(0编辑  收藏  举报