我更新了我的問題「Sizing a UILabel (in the iPhone SDK) to fit?」,並提供了一個建議解決方案的問題描述,但沒有得到答案。也許我會問一個新問題有更好的結果...如何獲取 - [NSString sizeWithFont:forWidth:lineBreakMode:]工作?
我把下面的代碼後斷點:
NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:17.0f] forWidth:260.0 lineBreakMode:UILineBreakModeWordWrap];
theSize.height
是21 theSize.width
是231什麼是我做錯了什麼?該高度不能以像素或行數爲單位。
請注意,[UIFont systemFontOfSize:17.0f]
是爲了指定默認字體。
更新:我改變了代碼:
CGSize constraintSize;
constraintSize.width = 260.0f;
constraintSize.height = MAXFLOAT;
NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
theSize.height
是273.這似乎更喜歡它。
我得到了完全相同的結果:21.任何人都知道該數字來自哪裏,還是僅僅基於該字體大小。愚蠢的方法。 – ZaBlanc 2011-10-07 20:24:12
如果您使用的是UITextView,請在這裏查看我的答案:http://stackoverflow.com/questions/2330939/sizewithfont-doesnt-give-correct-height-for-uitextview-if-there-is-a -long-strin/15408198#15408198 – domsom 2013-03-14 11:40:17