iOS获取Label高度的几种方法与对比

2025-05-29 0 15

介绍

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

一、sizeThatFits

使用 view sizeThatFits 方法。

?

1

2
// return 'best' size to fit given size. does not actually resize view. Default is return existing view size

- (CGSize)sizeThatFits:(CGSize)size;

例子:

?

1

2

3

4

5
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 不会。

?

1

2
// calls sizeThatFits: with current view bounds and changes bounds size.

- (void)sizeToFit;

例子

?

1

2

3

4

5
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 方法。

?

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

例子

?

1

2

3

4

5

6
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 方法。

?

1

2

3
// 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,用来指明绘制字符串时的渲染选项。

各个选项如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
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 即可。

例子

?

1

2

3

4

5

6

7
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开发者们能有所帮助,如果有疑问大家可以留言交流。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS获取Label高度的几种方法与对比 https://www.kuaiidc.com/91850.html

相关文章

发表评论
暂无评论