objective-c - 右侧为 "padding"的 NSTextField

标签 objective-c cocoa nstextfield

我试图让“剩余字符通知”显示在我的圆形 NSTextField 中,我在 Interface Builder 的帮助下用两个 NSTextFields 得到了它,它看起来像这样:

alt text http://jeenaparadies.net/t/s/Twittia-NSTextField1.png

但当我写多一点时,它看起来像这样:

alt text http://jeenaparadies.net/t/s/Twittia-NSTextField2.png

我唯一能想到的是子类化 NSTextField 并用它做一些事情,这样它就不会在数字下绘制文本,但我不知道如何开始,真的需要一些帮助。

最佳答案

最简单的方法可能是继承 NSTextFieldCell 并覆盖 -drawInteriorWithFrame:inView:-selectWithFrame:inView:editor:delegate:start:length:.

您需要决定为计数分配多少空间并在缩减空间中绘制。尽管尚未在圆形文本字段中进行测试,但类似此示例代码的内容应该可以工作。

您可以在 Apple 的 PhotoSearch example code 中找到有关子类化 NSCell 的更多信息.

- (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];
}

关于objective-c - 右侧为 "padding"的 NSTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/670015/

相关文章:

iphone - 屏幕锁定时如何关闭我的 iPhone 应用程序?

iOS:__weak 与(弱)

iphone - UIImage 像素内存泄漏?

cocoa - cocoa 中是否有任何 API 可以将任何兼容的网页转换为更具可读性的 TextView ?

cocoa - 只有在没有关系的情况下才正确处理对象的删除?

objective-c - NSTextField子类textDidEndEditing : Deleting Text

ios - 将 CSV 文件保存到文档目录文件夹

cocoa - 如何在显示另一个属性字符串时将 NSTextField 绑定(bind)到原始字符串?

macos - 如何使用 NSFormatter 对 NSTextField 中的输入进行着色?

objective-c - Cocoa 中的引用计数