虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > IOS编程/Objective-C > iOS获取Label高度的几种方法与对比

iOS获取Label高度的几种方法与对比
类别:IOS编程/Objective-C   作者:码皇   来源:互联网   点击:

这篇文章主要介绍了给大家介绍了iOS获取Label高度的几种方法,包括 view的sizeThatFits 方法、view的sizeToFit方法、NSString的sizeWithAttributes方法和NSString 的 boundingRectWithSize 方法,文中不仅介绍四种方法的实现,还进行了对比,下面来一起看看吧。

介绍

在设置 UILabel 的 Frame 高度时,不能简单的设置为字体的 font size。否则会将字体的一部分裁剪掉。因为 UILabel 在不同的字体设置下,对 Frame 的高度要求也不一样,大多数情况下都比Font的高度设置要高一些。

一、sizeThatFits

使用 view sizeThatFits 方法。

    // return 'best' size to fit given size. does not actually resize view. Default is return existing view size- (CGSize)sizeThatFits:(CGSize)size;

例子:

    UILabel *testLabel = [[UILabel alloc] init];
    testLabel.font = [UIFont systemFontOfSize:30];
    testLabel.text = @"Today is a fine day";
    CGSize size = [testLabel sizeThatFits:CGSizeMake(200, 30)];
    NSLog(@"size = %@", NSStringFromCGSize(size));

输出:size = {246.33333333333334, 36}

二、sizeToFit

使用 view sizeToFit 方法。

注意:sizeToFit 会改变 view 原来的 bounds,而 sizeThatFits 不会。

    // calls sizeThatFits: with current view bounds and changes bounds size.- (void)sizeToFit;

例子

    UILabel *testLabel = [[UILabel alloc] init];
    testLabel.font = [UIFont systemFontOfSize:30];
    testLabel.text = @"Today is a fine day";
    [testLabel sizeToFit];
    NSLog(@"size = %@", NSStringFromCGSize(testLabel.frame.size));

输出:size = {246.33333333333334, 36}

三、sizeWithAttributes

使用 NSString sizeWithAttributes 方法。

    - (CGSize)sizeWithAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);

例子

    NSString *text = @"Today is a fine day";
    UIFont *font = [UIFont systemFontOfSize:30];
    CGSize size = [text sizeWithAttributes:@{
    NSFontAttributeName : font }
    ];
    NSLog(@"size = %@", NSStringFromCGSize(size));

输出: size = {246.3134765625, 35.80078125}

四、boundingRectWithSize

使用 NSString boundingRectWithSize 方法。

    // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing to a single line.// To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

参数的意义:

1、size

限制最大宽高, 虽然是自适应,但是需要限制最大的宽度和高度。

2、options

类型为 NSStringDrawingOptions,用来指明绘制字符串时的渲染选项。

各个选项如下:

    typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) {
    // The specified origin is the line fragment origin, not the base line origin // 整个文本将以每行组成的矩形为单位计算整个文本的尺寸 NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // Uses the font leading for calculating line heights // 使用字体的行间距来计算文本占用的范围,即每一行的底部到下一行的底部的距离计算 NSStringDrawingUsesFontLeading = 1 << 1, // Uses image glyph bounds instead of typographic bounds // 将文字以图像符号计算文本占用范围,而不是排版的边界 NSStringDrawingUsesDeviceMetrics = 1 << 3, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. // Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set. // 如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。 // 如果 NSStringDrawingUsesLineFragmentOrigin 没有设置,则该选项不生效 NSStringDrawingTruncatesLastVisibleLine NS_ENUM_AVAILABLE(10_5, 6_0) = 1 << 5, }
    NS_ENUM_AVAILABLE(10_0, 6_0);

三、attributes

应用于字符串的文本属性。

四、context

NSStringDrawingContext 类型,控制调整字间距和缩放的比例,用于文本绘制时使用。该参数传入 nil 即可。

例子

    NSString *text = @"Today is a fine day";
    UIFont *font = [UIFont systemFontOfSize:30];
    CGRect suggestedRect = [text boundingRectWithSize:CGSizeMake(800, MAXFLOAT) options:NSStringDrawingUsesFontLeading attributes:@{
    NSFontAttributeName : font }
    context:nil];
    NSLog(@"size = %@", NSStringFromCGSize(suggestedRect.size));

输出: size = {200, 35.80078125}

四种方式对比

在设置字体为 30 的情况下,前两种使用 view 的方法返回 size = {246.33333333333334, 36} ,后两种使用 NSString 的方法返回 size = {246.3134765625, 35.80078125} 。使用 view 方法比使用  NSString 方法的返回的值略大。

我猜测其原因都是因为,文本渲染引擎在渲染一行文本的时候都需要在label的顶部和底部预留一小部分空间,应该是出于排版美观方面的考量。

在显示不同的 font size 的字体时,获得的字符串高度比 font size 大的值是不同的。

比如 font size 为 13 时,算出高度为 16,font size 为 20 时,算出高度为 24。

所以平常设置 UILabel 高度的时候,也不能简单的在 font height 基础之上加随意值。

总结

以上就是这篇文章的全部内容了,希望本文的内容对给位iOs开发者们能有所帮助,如果有疑问大家可以留言交流。

相关热词搜索: ios 获取label的高度 ios计算label高度 io