简体   繁体   English

更改NSTextField的边框“发光”颜色

[英]Change border “glow” color of NSTextField

I have an NSText field in MainMenu.xib and I have an action set to validate it for an email address. 我在MainMenu.xib中有一个NSText字段,我有一个操作集来验证它是否为电子邮件地址。 I want the NSTexFields border color (That blue glow) to be red when my action returns NO and green when the action returns YES. 我希望NSTexFields边框颜色(蓝色光晕)在我的动作返回NO时为红色,在动作返回YES时为绿色。 Here is the action: 这是行动:

-(BOOL) validEmail:(NSString*) emailString {
    NSString *regExPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";
    NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern     options:NSRegularExpressionCaseInsensitive error:nil];
    NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];
    NSLog(@"%ld", regExMatches);
    if (regExMatches == 0) {
        return NO;
    } else
        return YES;
}  

I call this function and setting the text-color right now, but I would like to set the NSTextField's glow color instead. 我现在调用此函数并设置文本颜色,但我想设置NSTextField的发光颜色。

- (void) controlTextDidChange:(NSNotification *)obj{
    if ([obj object] == emailS) {
        if ([self validEmail:[[obj object] stringValue]]) {
            [[obj object] setTextColor:[NSColor colorWithSRGBRed:0 green:.59 blue:0 alpha:1.0]];
            [reviewButton setEnabled:YES];
        } else {
            [[obj object] setTextColor:[NSColor colorWithSRGBRed:.59 green:0 blue:0 alpha:1.0]];
            [reviewButton setEnabled:NO];
        }
    }
}

I am open to sub-classing NSTextField, but the cleanest way to do this would be greatly appreciated! 我愿意对NSTextField进行子类化,但是最干净的方法非常感谢!

我对Swift 2的解决方案是


    @IBOutlet weak var textField: NSTextField!

... // === set border to red === // if text field focused right now NSApplication.sharedApplication().mainWindow?.makeFirstResponder(self) // disable following focusing textField.focusRingType = .None // enable layer textField.wantsLayer = true // change border color textField.layer?.borderColor = NSColor.redColor().CGColor // set border width textField.layer?.borderWidth = 1

The way to go about this is to subclass NSTextFieldCell and draw your own focus ring. 解决这个问题的方法是NSTextFieldCell并绘制自己的焦点环。 As far as I can tell there's no way to tell the system to draw the focus ring using your own color, so you'll have to call setFocusRingType:NSFocusRingTypeNone , check if the control has first responder status in your drawing method, and if so draw a focus ring using your own color. 据我所知,没有办法告诉系统使用你自己的颜色绘制聚焦环,所以你必须调用setFocusRingType:NSFocusRingTypeNone ,检查控件是否在绘图方法中具有第一响应者状态,如果是使用自己的颜色绘制聚焦环。

If you decide to use this approach remember that the focus ring is a user defined style (blue or graphite), and there's no guarantee future versions of OSX won't allow the user to change the standard color to red or green. 如果您决定使用此方法,请记住聚焦环是用户定义的样式(蓝色或石墨),并且无法保证OSX的未来版本不允许用户将标准颜色更改为红色或绿色。 It's also likely the focus ring drawing style will change in future versions of OSX, at which point you'll have to update your drawing code in order for things to look right. 在未来版本的OSX中,焦点环绘图样式也可能会发生变化,此时您必须更新绘图代码才能使事情看起来正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM