6

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

ありがとう!

4

6 に答える 6

13

これを試してください:

bounds = [value boundingRectWithSize:size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes];
于 2012-01-15T14:29:53.383 に答える
6

新しいNSExtendedStringDrawingカテゴリAPI(NSStringDrawingOptions引数を持つメソッド)は、単一行モードで動作します。マルチラインモードで測定/レンダリングする場合は、NSStringDrawingUsesLineFragmentOriginを指定します。

于 2011-03-04T19:05:46.557 に答える
1

文字列を特定のサイズに制限する場合は、を使用します-[NSString boundingRectWithSize:options:attributes:].size返されるのNSRectはあなたが探しているサイズです。

于 2011-02-17T19:45:42.017 に答える
1

編集: Lion以降では通常の方法で物事を行うことができるはずです。以下に説明する問題が修正されました。


現在のMacOSXAPI間でテキストを正確に測定する方法はありません。

動作することを約束しているが動作しないAPIがいくつかあります。それはそのうちの1つです。この目的のためのCoreTextの機能は別のものです。一部のメソッドは、近いが間違った結果を返します。まったく意味がないように見えるいくつかの結果が返されます。これらについてはまだバグを報告していませんが、提出する場合は、レーダー番号を編集してこの回答にします。バグも報告し、小さなサンプルプロジェクトにコードを含める必要があります。

[どうやら私はすでにコアテキスト1:8666756を提出しました。それは不特定の他のバグの複製として閉じられています。Cocoaについては、Daveが提案した方法について9022238を提出し、明日2つのNSLayoutManagerバグを提出する予定です。]

これは私が見つけた最も近い解決策です。

于 2011-02-18T01:50:37.433 に答える
1

これは、boundingRectWithSizeを使用してこれを行う方法のより完全な例です。

// get the text field
NSTextField* field = (NSTextField*)view;

// create the new font for the text field
NSFont* newFont = [NSFont fontWithName:@"Trebuchet MS" size:11.0];

// set the font on the text field
[field setFont:newFont];

// calculate the size the textfield needs to be in order to display the text properly
NSString* text      = field.stringValue;
NSInteger maxWidth  = field.frame.size.width;
NSInteger maxHeight = 20000;
CGSize constraint   = CGSizeMake(maxWidth, maxHeight);
NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:NSFontAttributeName,newFont, nil];
NSRect newBounds    = [text boundingRectWithSize:constraint
                                         options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attrs];

// set the size of the text field to the calculated size
field.frame = NSMakeRect(field.frame.origin.x, field.frame.origin.y, field.frame.size.width, newBounds.size.height);

もちろん、追加情報については、Appleのドキュメントを参照してください。

于 2013-12-12T23:32:33.317 に答える
-4

ドキュメントでを検索するNSStringと、「NSStringアプリケーションキット追加リファレンス」が見つかります。これは、UIKitの対応するものとほとんど同じです。

-[NSString sizeWithAttributes:]

あなたが探している方法です。

于 2011-02-17T19:33:53.323 に答える