Swift/OSX - Disabling Focus Ring on customized NSSecureTextField

1k views Asked by At

I'm trying out some styling of NSTextFields to get Single Line Text Fields like the Material Design ones. With normal NSTextFields I have no problems, it works out pretty well. I'm subclassing the NSTextFieldCell and draw my custom cell (simply with a 1px border at the bottom).

The code is like this:

override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView) {
        //let path:NSBezierPath = NSBezierPath.init(rect: cellFrame)
        //path.fill()
        var customRect = cellFrame
        customRect.origin.y = cellFrame.size.height - 1
        customRect.origin.x = 0;
        let usedColor:NSColor = NSColor.grayColor()
        usedColor.setFill()
        let path = NSBezierPath.init(rect: customRect)
        path.fill()
        
        super.drawWithFrame(cellFrame, inView: controlView);
    }

I'm adding the subclass in the interface builder and assign it to my TextField(Cell). Everything works fine with normal textfields.

But now I want to do the same with NSSecureTextFields, but the outcome is weird. The focus ring is visible, even though I set it to NONE.

The source code of the NSSecureTextFieldCell is the same as the one above (of course with the difference that I subclassed NSSecureTextFieldCell and not NSTextFieldCell), but why doesn't it show me the line at the bottom of the cell? And why do I get the damn focus ring when I assign my CustomCell-Class to the Cell? I just don't understand it and that makes me nuts.

Download Xcode Project File here (36 KB)

2

There are 2 answers

0
Barath On

You can use the following code to remove the focus ring in the viewDidLoad

self.SecuredTextField.focusRingType = NSFocusRingType.None

0
Reda Lemeden On

I had the same issue and I solved it by using a standard NSTextField with a NSSecureTextFieldCell subclass as its cell class.