首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >右边有"padding“的NSTextField

右边有"padding“的NSTextField
EN

Stack Overflow用户
提问于 2009-03-21 21:14:48
回答 1查看 5.2K关注 0票数 6

我试图让“剩余的字符通知”显示在我的四舍五入的NSTextField中,在界面生成器的帮助下,我用两个NSTextFields得到了它,它看起来已经是这样的:

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

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-03-22 09:10:16

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

您需要决定为您的计数分配多少空间,并在缩写空间中绘制。像这样的示例代码应该可以工作,尽管这还没有在四舍五入的文本字段中进行测试。

你可以在苹果的PhotoSearch example code中找到更多关于NSCell子类化的信息。

代码语言:javascript
复制
- (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];
}
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/670015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档