Swift NSTextField文本消失

时间:2017-06-06 17:35:03

标签: swift macos nstextfield nstrackingarea

这个问题可能存在于Objective-c的6岁帖子中。我没有找到最近的答案或问题,或者用Swift编写的答案或问题。

我正在使用故事板,我已经将NSTextField子类化了。出于某种原因,当我单击该字段时,占位符显示,当我输入文本并单击时,文本消失。文本实际上就在那里,因为当我再次单击时,输入的文本仍然存在。似乎某层渲染问题是图层覆盖了文本值?很奇怪。我正在使用NSTrackingArea并添加CALayer

以下是所发生情况的屏幕截图: enter image description here

这是我的代码:

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 个答案:

答案 0 :(得分:0)

我找到了答案。我删除了let textFieldLayer = CALayer()以及self.layer = textFieldLayer,因为这些似乎是在文本字段顶部添加了一个不必要的图层。这是我的工作版的完整代码:

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
}

}