Swift NSTextField text disappears

孤街醉人 提交于 2020-02-05 02:33:05

问题


This questions MIGHT exist on a 6 year old post in objective-c. I haven't found a recent answer or question that works, or one that is written in Swift.

I'm using storyboards and I have subclassed NSTextField. For some reason when I click into the field, the placeholder shows and when I enter text and click out, the text disappears. The text is actually there, because when I click back in, the entered text persists. It seems that it's some type of rendering issue that a layer is covering up the text value? Very strange. I'm using NSTrackingArea and also adding CALayer.

Here's a screenshot of what happens:

Here's my code:

class NowTextField: NSTextField {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // required nonsense
        let textFieldLayer = CALayer()
        let textFieldRect = NSRect(x: 0, y: 0, width: 120, height: 32)
        let textFieldTrackingArea = NSTrackingArea.init(rect: textFieldRect, options: [NSTrackingAreaOptions.activeInActiveApp, NSTrackingAreaOptions.mouseEnteredAndExited], owner: self, userInfo: nil)
        self.wantsLayer = true
        self.layer = textFieldLayer
        self.addTrackingArea(textFieldTrackingArea)

        // styling
        self.textColor = NSColor.darkGray
        self.layer?.cornerRadius = 4
        self.layer?.backgroundColor = NSColor.white.cgColor
        self.layer?.borderColor = NSColor.lightGray.cgColor
        self.layer?.borderWidth = 2
        self.drawsBackground = false
    }

    override func mouseEntered(with event: NSEvent) {
        self.textColor = NSColor.darkGray
        self.layer?.cornerRadius = 4
        self.layer?.backgroundColor = NSColor.white.cgColor
        self.layer?.borderColor = NSColor.highlightBlue.cgColor
        self.layer?.borderWidth = 2
        self.drawsBackground = false
    }

    override func mouseExited(with event: NSEvent) {
        self.textColor = NSColor.darkGray
        self.layer?.cornerRadius = 4
        self.layer?.backgroundColor = NSColor.white.cgColor
        self.layer?.borderColor = NSColor.lightGray.cgColor
        self.layer?.borderWidth = 2
        self.drawsBackground = false
    }
}

回答1:


I found the answer. I deleted let textFieldLayer = CALayer() as well as self.layer = textFieldLayer as these seemed to be adding an unnecessary layer on top of the text field. Here's my full code for the working version:

class NowTextField: NSTextField {

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)

    // required nonsense
    let textFieldRect = NSRect(x: 0, y: 0, width: 120, height: 32)
    let textFieldTrackingArea = NSTrackingArea.init(rect: textFieldRect, options: [NSTrackingAreaOptions.activeInActiveApp, NSTrackingAreaOptions.mouseEnteredAndExited], owner: self, userInfo: nil)
    self.wantsLayer = true
    self.addTrackingArea(textFieldTrackingArea)

    // styling
    self.textColor = NSColor.darkGray
    self.layer?.cornerRadius = 4
    self.layer?.backgroundColor = NSColor.white.cgColor
    self.layer?.borderColor = NSColor.lightGray.cgColor
    self.layer?.borderWidth = 2
    self.drawsBackground = false
}

override func mouseEntered(with event: NSEvent) {
    self.textColor = NSColor.darkGray
    self.layer?.cornerRadius = 4
    self.layer?.backgroundColor = NSColor.white.cgColor
    self.layer?.borderColor = NSColor.highlightBlue.cgColor
    self.layer?.borderWidth = 2
    self.drawsBackground = false
}

override func mouseExited(with event: NSEvent) {
    self.textColor = NSColor.darkGray
    self.layer?.cornerRadius = 4
    self.layer?.backgroundColor = NSColor.white.cgColor
    self.layer?.borderColor = NSColor.lightGray.cgColor
    self.layer?.borderWidth = 2
    self.drawsBackground = false
}

}



来源:https://stackoverflow.com/questions/44396370/swift-nstextfield-text-disappears

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!