6

「残りの文字の通知」を丸めた NSTextField 内に表示しようとしていますが、Interface Builder の助けを借りて 2 つの NSTextField でそれを取得しましたが、すでに次のようになっています。

代替テキスト http://jeenaparadies.net/t/s/Twittia-NSTextField1.png

しかし、もう少し書くと、次のようになります。

代替テキスト http://jeenaparadies.net/t/s/Twittia-NSTextField2.png

私が考えることができる唯一のことは、 NSTextField をサブクラス化し、それを使って何かをして、数字の下にテキストを描画しないようにすることですが、開始方法がわかりません。本当に助けが必要です。

4

1 に答える 1

13

最も簡単な方法は、おそらくandをサブクラス化NSTextFieldCellしてオーバーライドすることです。-drawInteriorWithFrame:inView:-selectWithFrame:inView:editor:delegate:start:length:

カウントに割り当てるスペースを決定し、省略されたスペースに描画する必要があります。このサンプル コードのようなものは動作するはずですが、これは丸みを帯びたテキスト フィールドではテストされていません。

サブクラス化の詳細についてNSCellは、Apple のPhotoSearch サンプル コードを参照してください。

- (void)drawInteriorWithFrame:(NSRect)bounds inView:(NSView *)controlView {
    NSRect titleRect = [self titleRectForBounds:bounds];
    NSRect countRect = [self countAreaRectForBounds:bounds];

    titleRect = NSInsetRect(titleRect, 2, 0);

    NSAttributedString *title = [self attributedStringValue];
    NSAttributedString *count = [self countAttributedString];

    if (title)
        [title drawInRect:titleRect];

    [count drawInRect:countRect];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
    NSRect selectFrame = aRect;
    NSRect countRect = [self countAreaRectForBounds:aRect];

    selectFrame.size.width -= countRect.size.width + PADDING_AROUND_COUNT;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__textChanged:) name:NSTextDidChangeNotification object:textObj];
    [super selectWithFrame:selectFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}

- (void)endEditing:(NSText *)editor {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSTextDidChangeNotification object:editor];

    [super endEditing:editor];
}

- (void)__textChanged:(NSNotification *)notif {
    [[self controlView] setNeedsDisplay:YES];
}
于 2009-03-22T09:10:16.177 に答える