将不可编辑的NSTextField作为标签添加到另一个NSTextField中 [英] Adding non editable NSTextField as a label into another NSTextField

查看:215
本文介绍了将不可编辑的NSTextField作为标签添加到另一个NSTextField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自定义的 NSTextField ,该a)在激活时更改颜色,b)在左上角具有标签。

I am trying to implement a custom NSTextField that a) changes color when active and b) has a label in the top left hand corner.

我有以下实现:

TestTextField.h

@interface TestTextField : NSTextField

- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset;
...
@end

TestTextField中.m

@interface TestTextField() {

    BOOL _thisFieldIsActive;
}

@property (nonatomic, strong) NSTextField *label;

@end

@implementation TestTextField

- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset {

    self = [super initWithFrame:NSZeroRect];
    if (self) {

        _thisFieldIsActive = NO;

        [self setFocusRingType:NSFocusRingTypeNone];

        // small label top left
        _label = [[NSTextField alloc] initWithFrame:NSZeroRect];
        _label.stringValue = label;

        if (preset) {
            self.stringValue = preset;
        }
        else {
            self.stringValue = @"0";
        }

        [self layoutUI];

    }
    return self;

}
- (void)turnActiveOff {
    [self toggleActive:NO];
}

- (void)toggleActive:(BOOL)active {

    _thisFieldIsActive = active;

    if (_thisFieldIsActive) {
        self.backgroundColor = [NSColor blueColor];
        self.textColor = [NSColor whiteColor];
        _label.textColor = [NSColor whiteColor];
    }
    else {
        self.backgroundColor = [NSColor clearColor];
        self.textColor = [NSColor blackColor];
        _label.textColor = [NSColor grayColor];
    }

}
- (BOOL)becomeFirstResponder {

    NSLog(@"BecomeFirstResponder");

    [self selectText:self];
    [self toggleActive:YES];

    return [super becomeFirstResponder];
}

- (void)textDidEndEditing:(NSNotification *)notification {
    NSLog(@"DidEndEditing");

    [self toggleActive:NO];
    [super textDidEndEditing:notification];
}

- (void)layoutUI {

    self.alignment = NSRightTextAlignment;
    self.font = [NSFont fontWithName:@"HelveticaNeue-Light" size:32.0f];
    self.layer.borderColor = [NSColor whiteColor].CGColor;
    self.layer.borderWidth = 1.0f;
    [self.layer setCornerRadius:4.0f];

    // small label top left
    _label.font = [NSFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
    _label.alignment = NSLeftTextAlignment;
    _label.textColor = [NSColor grayColor];
    _label.stringValue = [_label.stringValue uppercaseString];
    _label.selectable = NO;
    _label.editable = NO;
    _label.drawsBackground = NO;
    _label.bezeled = NO;
    [self addSubview:_label];

    NSDictionary *metrics = @{@"borderPadding": @5};

    _label.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(borderPadding)-[_label(100)]" options:0 metrics:metrics views:@{ @"_label" : _label }]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(borderPadding)-[_label(30)]" options:0 metrics:metrics views:@{ @"_label" : _label }]];

}

在ViewController中,我通过简单地调用自定义来实现这些TestTextField initFullWidthWithLabel:andPreset:方法并将其作为子视图添加到VCs view

In my ViewController I implement these TestTextFields by simply calling the custom initFullWidthWithLabel:andPreset: method and adding them as a subview to the VCs view

我可以看到标签位置正确,但是,一旦该字段变为活动状态并且设置了backgroundColor,它似乎就会掩盖标签。如何确保标签在顶部?
即使关闭了backgorund着色,标签仍处于隐藏状态。

I can see that the label gets positioned correctly, however as soon as the the field becomes active and the backgroundColor is set, it seems to cover up the label. How can I make sure the label stays on top? Even when the backgorund coloring is turned off, the label remains hidden.

谢谢

推荐答案

解决方案的核心是使用自定义子类 NSTextFieldCell 的一种方法:

The core of the solution was to use a custom subclassed NSTextFieldCell with one method:

- (NSRect)drawingRectForBounds:(NSRect)rect {
    NSRect rectInset = NSMakeRect(rect.origin.x + 100.0f, rect.origin.y, rect.size.width - 100.0f, rect.size.height);
    return [super drawingRectForBounds:rectInset];
}

这篇关于将不可编辑的NSTextField作为标签添加到另一个NSTextField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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