Swift NSTextField 文本消失

标签 swift macos nstextfield nstrackingarea

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

我正在使用 Storyboard并且我已经子类化了 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
    }
}

最佳答案

我找到了答案。我删除了 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
}

关于Swift NSTextField 文本消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44396370/

相关文章:

c - 具有嵌套函数的 OSX "Illegal instruction 4"

macos - 在Mac OS X上更新youtube-dl

macos - SCTP 在 OSX 上缺少包含文件?

cocoa - 当 NSTextField 内容增加时如何调整 NSTextField 大小

swift - 如何为了测试而覆盖 Observable<T>?

json - Swift Json 如何在没有顶级 key 和自动生成的 key 的情况下进行解码

ios - CollectionView ItemHeight 比 Collectionview 高度大

ios - MKMapView 的状态恢复稍微缩小

objective-c - 抑制 NSTextField 的文本完成下拉列表

cocoa - 如何在非事件状态下自定义 NSTextField/NSTextView 的选定文本颜色