iOS獲取Label高度的幾種方法與對(duì)比
介紹
在設(shè)置 UILabel 的 Frame 高度時(shí),不能簡(jiǎn)單的設(shè)置為字體的 font size。否則會(huì)將字體的一部分裁剪掉。因?yàn)?UILabel 在不同的字體設(shè)置下,對(duì) Frame 的高度要求也不一樣,大多數(shù)情況下都比Font的高度設(shè)置要高一些。
一、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 會(huì)改變 view 原來(lái)的 bounds,而 sizeThatFits 不會(huì)。
// 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);
參數(shù)的意義:
1、size
限制最大寬高, 雖然是自適應(yīng),但是需要限制最大的寬度和高度。
2、options
類型為 NSStringDrawingOptions,用來(lái)指明繪制字符串時(shí)的渲染選項(xiàng)。
各個(gè)選項(xiàng)如下:
typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) {
// The specified origin is the line fragment origin, not the base line origin
// 整個(gè)文本將以每行組成的矩形為單位計(jì)算整個(gè)文本的尺寸
NSStringDrawingUsesLineFragmentOrigin = 1 << 0,
// Uses the font leading for calculating line heights
// 使用字體的行間距來(lái)計(jì)算文本占用的范圍,即每一行的底部到下一行的底部的距離計(jì)算
NSStringDrawingUsesFontLeading = 1 << 1,
// Uses image glyph bounds instead of typographic bounds
// 將文字以圖像符號(hào)計(jì)算文本占用范圍,而不是排版的邊界
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.
// 如果文本內(nèi)容超出指定的矩形限制,文本將被截去并在最后一個(gè)字符后加上省略號(hào)。
// 如果 NSStringDrawingUsesLineFragmentOrigin 沒(méi)有設(shè)置,則該選項(xiàng)不生效
NSStringDrawingTruncatesLastVisibleLine NS_ENUM_AVAILABLE(10_5, 6_0) = 1 << 5,
} NS_ENUM_AVAILABLE(10_0, 6_0);
三、attributes
應(yīng)用于字符串的文本屬性。
四、context
NSStringDrawingContext 類型,控制調(diào)整字間距和縮放的比例,用于文本繪制時(shí)使用。該參數(shù)傳入 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}
四種方式對(duì)比
在設(shè)置字體為 30 的情況下,前兩種使用 view 的方法返回 size = {246.33333333333334, 36} ,后兩種使用 NSString 的方法返回 size = {246.3134765625, 35.80078125} 。使用 view 方法比使用 NSString 方法的返回的值略大。
我猜測(cè)其原因都是因?yàn)?,文本渲染引擎在渲染一行文本的時(shí)候都需要在label的頂部和底部預(yù)留一小部分空間,應(yīng)該是出于排版美觀方面的考量。
在顯示不同的 font size 的字體時(shí),獲得的字符串高度比 font size 大的值是不同的。
比如 font size 為 13 時(shí),算出高度為 16,font size 為 20 時(shí),算出高度為 24。
所以平常設(shè)置 UILabel 高度的時(shí)候,也不能簡(jiǎn)單的在 font height 基礎(chǔ)之上加隨意值。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)給位iOs開發(fā)者們能有所幫助,如果有疑問(wèn)大家可以留言交流。
相關(guān)文章
iOS評(píng)分(評(píng)價(jià))星星圖打分功能
這篇文章主要介紹了iOS評(píng)分(評(píng)價(jià))星星圖打分功能,評(píng)分視圖分為展示和評(píng)分兩種,具體詳情大家可以通過(guò)本文詳細(xì)學(xué)習(xí)2016-11-11
IOS小組件實(shí)現(xiàn)時(shí)鐘按秒刷新功能
小組件運(yùn)行在單獨(dú)的進(jìn)程,如果異常會(huì)導(dǎo)致小組件進(jìn)程卡死了,一個(gè)小組件出問(wèn)題,其他小組件都不刷新了。既然刷新這么難控制,怎么實(shí)現(xiàn)數(shù)字時(shí)鐘按秒刷新呢?接下來(lái)通過(guò)代碼給大家介紹下ios小組件刷新功能的實(shí)現(xiàn),一起看看吧2021-05-05
iOS開發(fā)中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片瀏覽器的實(shí)例講解
這篇文章主要介紹了iOS開發(fā)中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片瀏覽器的實(shí)例講解,代碼基礎(chǔ)傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
IOS 時(shí)間和時(shí)間戳之間轉(zhuǎn)化示例
我們經(jīng)常從服務(wù)器后臺(tái)拿到時(shí)間戳的時(shí)間,以下代碼可以實(shí)現(xiàn)將時(shí)間戳轉(zhuǎn)為可讀的時(shí)間格式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01

