AppKit(Mac OS XのCocoa用)にUIKitと同じことを行う同等の方法はあります[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で、高さは1行の高さよりも大きくなると思いますが、次のようになります。
height: 14.000000, width: 466.619141
ありがとう!