源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-03-24 21:06 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS获取Label高度的几种方法与对比
[b]介绍[/b] 在设置 UILabel 的 Frame 高度时,不能简单的设置为字体的 [code]font size[/code]。否则会将字体的一部分裁剪掉。因为 UILabel 在不同的字体设置下,对 Frame 的高度要求也不一样,大多数情况下都比Font的高度设置要高一些。 [b]一、sizeThatFits[/b] 使用 [code]view [/code]的 [code]sizeThatFits [/code]方法。
// return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size;
[b]例子:[/b]
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));
[b]输出:[/b][code]size = {246.33333333333334, 36}[/code] [b]二、sizeToFit[/b] 使用 [code]view [/code]的 [code]sizeToFit [/code]方法。 [b]注意:[/b][code]sizeToFit [/code]会改变 [code]view [/code]原来的 [code]bounds[/code],而 [code]sizeThatFits [/code]不会。
// calls sizeThatFits: with current view bounds and changes bounds size.
- (void)sizeToFit;
[b]例子[/b]
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));
[b]输出:[/b][code]size = {246.33333333333334, 36}[/code] [b]三、sizeWithAttributes[/b] 使用 [code]NSString [/code]的 [code]sizeWithAttributes [/code]方法。
- (CGSize)sizeWithAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
[b]例子[/b]
NSString *text = @"Today is a fine day";
UIFont *font = [UIFont systemFontOfSize:30];
CGSize size = [text sizeWithAttributes:@{
           NSFontAttributeName : font
           }];
NSLog(@"size = %@", NSStringFromCGSize(size));
[b]输出:[/b] [code]size = {246.3134765625, 35.80078125}[/code] [b]四、boundingRectWithSize[/b] 使用 [code]NSString [/code]的 [code]boundingRectWithSize [/code]方法。
// 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);
[b]参数的意义:[/b] [b]1、size[/b] 限制最大宽高, 虽然是自适应,但是需要限制最大的宽度和高度。 [b]2、options[/b] 类型为 [code]NSStringDrawingOptions[/code],用来指明绘制字符串时的渲染选项。 [b]各个选项如下:[/b]
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);
[b]三、attributes [/b] 应用于字符串的文本属性。 [b]四、context[/b] [code]NSStringDrawingContext [/code]类型,控制调整字间距和缩放的比例,用于文本绘制时使用。该参数传入 nil 即可。 [b]例子[/b]
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));
[b]输出:[/b] [code]size = {200, 35.80078125}[/code] [b]四种方式对比[/b] 在设置字体为 30 的情况下,前两种使用 [code]view [/code]的方法返回 [code]size = {246.33333333333334, 36} [/code],后两种使用 [code]NSString [/code]的方法返回 [code]size = {246.3134765625, 35.80078125}[/code] 。使用 [code]view [/code]方法比使用  [code]NSString [/code]方法的返回的值略大。 我猜测其原因都是因为,文本渲染引擎在渲染一行文本的时候都需要在label的顶部和底部预留一小部分空间,应该是出于排版美观方面的考量。 在显示不同的 font size 的字体时,获得的字符串高度比[code] font size [/code]大的值是不同的。 比如 [code]font size[/code] 为 13 时,算出高度为 16,[code]font size [/code]为 20 时,算出高度为 24。 所以平常设置 UILabel 高度的时候,也不能简单的在 font height 基础之上加随意值。 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对给位iOs开发者们能有所帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部