zoukankan      html  css  js  c++  java
  • 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,也在此感谢此博友的分享!

  • 相关阅读:
    @Autowired 注解是如何实现的?
    工作 3 年的同事不懂 isEmpty 和 isBlank 的区别,我真是醉了。。
    4 个单词,谷歌返回 16 个 SQL 注入漏洞...
    MySQL 更新不成功,事务问题搞清楚了吗?
    Python Web应用如何部署?
    mapbox加载postgis矢量切片
    tile2lon:地图瓦片编号与经纬度的换算关系
    shp2pgsql:将shapefile导入postgis数据库
    postgreSQL表添加ID自增列
    postgreSQL连接配置
  • 原文地址:https://www.cnblogs.com/iOS771722918/p/4464985.html
Copyright © 2011-2022 走看看