iOS擷取Label高度的幾種方法與對比_IOS

來源:互聯網
上載者:User

介紹

在設定 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開發人員們能有所協助,如果有疑問大家可以留言交流。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.