欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-20 20:37:16
介绍 在设置 uilabel 的 frame 高度时,不能简单的设置为字体的 font size。否则会将字体的一部分裁剪掉。因为 uilabel 在不同的字体设置下,对...

介绍

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

上一篇:

下一篇: