I have subclassed NSTextField with the following code:
import Cocoa class CustomSearchField: NSTextField { override func draw(_ dirtyRect: NSRect) { self.wantsLayer = true let textFieldLayer = CALayer() self.layer = textFieldLayer self.backgroundColor = NSColor.white self.layer?.backgroundColor = CGColor.white self.layer?.borderColor = CGColor.white self.layer?.borderWidth = 0 super.cell?.draw(withFrame: dirtyRect, in: self) } } class CustomSearchFieldCell: NSTextFieldCell { override func drawingRect(forBounds rect: NSRect) -> NSRect { let minimumHeight = self.cellSize(forBounds: rect).height let newRect = NSRect(x: rect.origin.x + 25, y: (rect.origin.y + (rect.height - minimumHeight) / 2) - 4, width: rect.size.width - 50, height: minimumHeight) return super.drawingRect(forBounds: newRect) } }
It all works fine, and he drew my NSTextField the way I wanted. The only problem is that as soon as I make some part of the interface the first responder (by clicking outside NSTextField), the text inside the NSTextField (placeholder or filled text) disappears. As soon as I click on it again, it disappears. Iβve been looking for silence for some time, but I canβt understand why this is happening. I just want the text to be visible all the time, instead of fading in and out.
This is due to CALayer, which I add to fulfill my style.
Whenever I run the same settings from viewDidLoad in a text box, it works like a charm. For example:
class ViewController: NSViewController { @IBOutlet weak var searchField: NSTextField! override func viewDidLoad() { initCustomSearchField() } private func initCustomSearchField() { searchField.wantsLayer = true let textFieldLayer = CALayer() searchField.layer = textFieldLayer searchField.backgroundColor = NSColor.white searchField.layer?.backgroundColor = CGColor.white searchField.layer?.borderColor = CGColor.white searchField.layer?.borderWidth = 0 searchField.delegate = self } }
cocoa swift subclass nstextfield
Wouter125
source share