具有“填充”的NSTextField在右边 [英] NSTextField with "padding" on the right

查看:284
本文介绍了具有“填充”的NSTextField在右边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得剩余的chars通知显示在我圆形的NSTextField和我得到它与两个NSTextFields的帮助下的Interface Builder,它已经看起来像:









我可以想到的唯一的事情是子类化NSTextField和做一些事情,所以它不绘制文本下的数字,但我不知道如何开始,需要真正一些帮助。

解决方案

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


I am trying to get the "remaining chars notice" to show up within my rounded NSTextField and I got it with two NSTextFields with help of the Interface Builder and it already looks like that:

But when I write a little bit more it looks like that:

The only thing I could think of is to subclass NSTextField and do something with it so it does not draw text under the number but I have no idea how to begin and need really some help with it.

解决方案

The simplest way is probably to subclass NSTextFieldCell and override -drawInteriorWithFrame:inView: and -selectWithFrame:inView:editor:delegate:start:length:.

You'll need to decide how much space to allocate for your count and draw in the abbreviated space. Something like this example code should work although this hasn't been tested in a rounded text field.

You can find more information about subclassing NSCell in Apple's PhotoSearch example code.

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

这篇关于具有“填充”的NSTextField在右边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆