これを実行する方法を探しましたが、答えが見つかりません。の長さを確認せずに、テキストがすでに切り捨て
られているかどうか(最後に...)を知りたいのですが。自分にツールチップを設定する必要があるかどうかを知るために、これを行う必要があります(単純なラベルのように機能します)。
テキストフィールドの長さを確認したくないのは、他の文字よりも多くのスペースを占める文字があるため、正確ではないため
です。ありがとうございます。NSTextfield
stringValue
NSTextfield
stringValue
4 に答える
NSTextView
の代わりにを使用することをお勧めしますNSTextField
。を使用すると、 プロパティを使用してNSTextView
を取得できます。コンテナーは(テキストが描画されるスペース) を教えてくれます。NSTextContainer
NSTextView
textContainer
containerSize
NSString
オブジェクトはメソッドに応答しますsizeWithAttributes:
。指定された属性で描画された場合、結果のNSSize
構造体を使用してテキストの幅を取得できます。NSAttributedString Application Kit Additions Referenceの「Constants」セクションを参照して、関連する属性を把握してください。
containerSize
幅が幅より小さい場合、sizeWithAttributes:
テキストは切り捨てられます。
編集:申し訳ありませんが、これは no の場合にのみ当てはまりlineFragmentPadding
ますが、デフォルトlineFragmentPadding
はゼロではありません。textContainer.lineFragmentPadding
から を減算するか、メソッドをcontainerSize.width
使用して に設定します。NSTextContainer
setLineFragmentPadding:
0
のサイズに関連するテキスト領域についていくつかの仮定を立て、それと組み合わせてメソッドNSTextField
を使用することもできると思いますがsizeWithAttributes:
NSString
、それはそれほどきれいではありません。
編集2:楕円を使用してテキストを切り捨てるというOPの関心に対処していないことに気付きました。以下のコード例では、NSTextView
. また、 を の中に入れてNSTextView
、 の外観を にもう少し似たコードを挿入することも考えました。ツールチップを表示するかどうかを決定するサイズのチェックを追加することは、上記の情報を使用して以下のコードに簡単に追加できます。NSTextField
NSBox
NSString* string = @"Hello World.";
// get the size the text will take up using the default font
NSSize textBounds = [string sizeWithAttributes:@{}];
// Create a border view that looks more or less like the border used around
// text fields
NSBox* borderView = [[NSBox alloc] initWithFrame:NSMakeRect(10, 10, 60, textBounds.height+4)];
[borderView setBoxType:NSBoxCustom];
[borderView setBorderType:NSBezelBorder];
[borderView setContentViewMargins:NSMakeSize(0, 0)];
[borderView setFillColor:[NSColor whiteColor]];
// Create the text view
NSTextView* textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 60, textBounds.height)];
[textView setTextContainerInset:NSMakeSize(2, 0)];
[textView.textContainer setLineFragmentPadding:0];
[textView setEditable:YES];
// Set the default paragraph style so the text is truncated rather than
// wrapped
NSMutableParagraphStyle* parStyle = [[NSMutableParagraphStyle alloc] init];
[parStyle setLineBreakMode:NSLineBreakByTruncatingTail];
// Do not let text get squashed to fit
[parStyle setTighteningFactorForTruncation:0.0];
[textView setDefaultParagraphStyle:parStyle];
[parStyle release];
// Set text
[textView setString:string];
// add NSTextView to border view
[borderView addSubview:textView];
[textView release];
// add NSBox to view you want text view displayed in
[self addSubview:borderView];
[borderView release];
テキストフィールドのフィールドエディタの枠を見ればできると思います。controlTextDidBeginEditing で幅を確認し、次に controlTextDidEndEditing で再度確認します。後者の値が大きい場合、テキストは切り捨てられています。以下は、テキスト フィールドのデリゲートに実装されています (initialWidth の ivar を作成しました)。
- (void)controlTextDidBeginEditing:(NSNotification *)aNotification {
if (! initialWidth)
initialWidth = ((NSTextView *)aNotification.userInfo[@"NSFieldEditor"]).frame.size.width;
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification {
if (initialWidth) { //Make sure that beginEditing is called first
NSInteger finalWidth = ((NSTextView *)aNotification.userInfo[@"NSFieldEditor"]).frame.size.width;
if (finalWidth - initialWidth > 1) NSLog(@"We have truncation");
NSLog(@"Final: %ld Initial: %ld", finalWidth,initialWidth);
}
}
これは、長い文字列を入力してから、再び収まるまで削除する場合を含め、ほとんどの場合に機能するようです。いくつかのケースでは、テキスト フィールドの末尾を 1 文字過ぎたときにログ メッセージが表示されましたが、編集が終了したときに省略記号が表示されませんでした。