簡體   English   中英

使用numberOfLines = 2的UILabel使用什么約束高度? (使用[NSString sizeWithFont ...])

[英]What constraint height to use for UILabel with numberOfLines=2? (using [NSString sizeWithFont…])

如何確定在[NSString sizeWithFont:constrainedToSize:...]中使用的constrainedToSize.height為行數有限的UILabel?

下面的約束高度是無限制的(MAXFLOAT),但是當限制為X行數時,用於標簽最大高度的是什么?

UILabel * label = [[UILabel alloc] init];
label.numberOfLines = 2;
label.text = @"Some really long text";

// what to use instead of MAXFLOAT?
CGSize constrainSize = CGSizeMake(285, MAXFLOAT);

CGSize size = [label.text
               sizeWithFont: [UIFont boldSystemFontOfSize:17.0]
               constrainedToSize:constrainSize
               lineBreakMode:NSLineBreakByWordWrapping
               ];

非常感謝!

如果您已經知道標簽是2行,那么使用:

CGFloat maxHeight = label.font.lineHeight * 2;
CGSize constrainSize = CGSizeMake(285, maxHeight);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM