[英]How to use NSString's sizeWithFont and drawInRect to workout how much of a string to draw
[英]How do I properly use NSString sizeWithFont constratinedToSize with NIAttributedLabel
我已經遍歷了關於SO上此方法的每篇文章的感覺。 許多張貼者的答案是將一個非常大的值用作高度限制。
但是,對我來說這是行不通的。 這是我的代碼:
//Create the contentLabel Label
CGSize size = [contentText sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:CGSizeMake(286, 9999) lineBreakMode:UILineBreakModeWordWrap];
//Create the content label with its position 7 pixels below the title
contentLabel = [[NIAttributedLabel alloc] initWithFrame:
CGRectMake(7, titleContainerView.frame.origin.y + 7, 286, size.height)];
//Set the provided text and the font
contentLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
contentLabel.numberOfLines = 0;
contentLabel.lineBreakMode = UILineBreakModeWordWrap;
contentLabel.text = contentText;
我正在使用
NIAttributedLabel
,認為這實際上不會產生效果,因為返回我認為是錯誤大小的方法是
NSString
一部分。
我確實在Apple的文檔中讀到,該方法有時會截斷字符串,盡管我認為那是大高度限制的原因
編輯:我發現這是NIAttributedLabel
的問題,如果我使用常規的UILabel則可以正常工作。 這是兩個源字符串和相應的屏幕截圖,第一個演示我的問題,另一個確定沒問題:
"Buying a Mobile\\nHello - I'd like a Motorola Defy with a Smartphone 60 Plan.\\nBroadband Problem\\nMy Broadband's out. I've tested the router and cables and ruled out my equipment. Is there a problem at your end?"
"Buying a Mobile\\nI\\'m Mrs Sina Manu-Harris. My account number is 156205169. I\\'m going overseas in 6 months time on the 2nd of September and I\\'d like to get organized in advance and buy a new mobile phone.\\nBroadband Problem\\nGood afternoon. It's Mrs Sina Manu-Harris here. My account number is 156205169. My Broadband isn't working. I've checked my network and phone cables and I've also checked my filters."
NimbusKit 1.0提供了一種計算NIAttributedLabel高度的新方法:NISizeOfAttributedStringConstrainedToSize()
我曾經使用過sizeWithFont:,並且遇到過與您相同的問題。 現在,新方法非常適合我(我的標簽中沒有任何圖像)
您不能使用與CoreText相同的字形放置算法來依賴NSString。 調整NIAttributedLabel的大小時,建議您使用標簽的sizeToFit
和sizeThatFits:
方法,因為它們使用CoreText來確定標簽的理想尺寸。
請檢查字符串@“ T \\ nT \\ nT”,它只會打印@“ T \\ nT \\ n”。 似乎_textFrame僅顯示兩條可見線,而不是三條。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.