我已經走過了什麼感覺就像這個方法在SO上的每一篇文章。 很多海報的答案都是把一個非常大的值作爲高度限制。我如何正確使用NSString sizeWithFont constratinedToSize與NIAttributedLabel
但是,對我來說這是行不通的。這是我的代碼:
//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
一部分。
我沒有蘋果的文檔中讀取的方法有時會截斷字符串,但我認爲多數民衆贊成在大高度約束是爲
編輯: 我發現它是一個問題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."
描述它如何不工作。截圖會很有幫助。你是否一步一步查看標籤的最終高度是否確實是'sizeWithFont'中確定的高度?也許NIAttributedLabel實現中有一些怪癖。 – 2012-07-13 08:15:00
我甚至有UILabel類似的問題... – Bharathi 2012-07-13 09:53:44