Swift NSTextField文本消失
这个问题可能存在于objective-c中一个6岁的职位上。我最近还没有找到一个有效的答案或问题,也没有一个是用Swift写的 我使用的是故事板,我有子类Swift NSTextField文本消失,swift,macos,nstextfield,nstrackingarea,Swift,Macos,Nstextfield,Nstrackingarea,这个问题可能存在于objective-c中一个6岁的职位上。我最近还没有找到一个有效的答案或问题,也没有一个是用Swift写的 我使用的是故事板,我有子类NSTextField。出于某种原因,当我单击该字段时,占位符会显示,当我输入文本并单击退出时,文本会消失。文本实际上在那里,因为当我单击“后退”时,输入的文本仍然存在。这似乎是某种类型的渲染问题,层覆盖了文本值?很奇怪。我正在使用NSTrackingArea并添加CALayer 以下是发生的情况的屏幕截图: 这是我的密码: class No
NSTextField
。出于某种原因,当我单击该字段时,占位符会显示,当我输入文本并单击退出时,文本会消失。文本实际上在那里,因为当我单击“后退”时,输入的文本仍然存在。这似乎是某种类型的渲染问题,层覆盖了文本值?很奇怪。我正在使用NSTrackingArea
并添加CALayer
以下是发生的情况的屏幕截图:
这是我的密码:
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
}
}在init方法或
awakeFromNib()
中设置文本字段,而不是在每个绘图中设置。不幸的是,这并没有解决问题。添加到awakeFromNib()时,一切都是一样的