在AppKit(對於Mac OS X上的Cocoa)中是否有與UIKit的[NSString sizeWithFont:constrainedToSize:]
做同樣的事情的方法?UIKit在AppKit中的[NSString sizeWithFont:constrainedToSize:]
如果不是這樣,我怎麼能得到足夠的空間來呈現一個特定的字符串被限制在一個寬度/高度?
更新:下面是我使用的代碼片段,我期望會產生我之後的結果。
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize: [NSFont smallSystemFontSize]], NSFontAttributeName,
[NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName,
nil];
NSSize size = NSMakeSize(200.0, MAXFLOAT);
NSRect bounds;
bounds = [@"This is a really really really really really really really long string that won't fit on one line"
boundingRectWithSize: size
options: NSStringDrawingUsesFontLeading
attributes: attributes];
NSLog(@"height: %02f, width: %02f", bounds.size.height, bounds.size.width);
我期望的輸出寬度爲200,高度將是東西比一個單行的高度,但是它產生:
height: 14.000000, width: 466.619141
謝謝!
重複:http://stackoverflow.com/questions/ 2349692 /最新最等效sizewithfont法換的-MAC。有沒有人搜索這些日子? –
我搜索,但令人驚訝的是,我沒有找到那一個。無論如何,sizeWithAttributes:不,afaik,支持限制寬度。 –
這不是重複的,因爲這個約束元素在其他問題中缺少。 –