当前位置:  开发笔记 > 前端 > 正文

NSTextField右侧有"填充"

如何解决《NSTextField右侧有"填充"》经验,为你挑选了1个好方法。

我试图在我的圆形NSTextField中显示"剩余字符通知",并在Interface Builder的帮助下使用两个NSTextField获得它,它看起来像是这样:

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

但是当我写一点点时,它看起来像那样:

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

我唯一能想到的是继承NSTextField并使用它做一些事情,因此它不会在数字下面绘制文本,但我不知道如何开始并需要真正的帮助.



1> Ashley Clark..:

最简单的方法可能是子类化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];
}


现在的链接是:https://developer.apple.com/library/prerelease/content/samplecode/PhotoSearch/Introduction/Intro.html
推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有