2017-06-06 98 views
0

这个问题可能存在于一个6岁的职位在objective-c。我还没有找到最近的答案或问题,或者用Swift编写的问题。斯威夫特NSTextField文本消失

我正在使用故事板,并且我有子类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 
    } 
} 
+0

设置文本字段中的init方法或'awakeFromNib()的',而不是在每一个'draw'。 – Willeke

+0

不幸的是,这并没有解决问题。添加到'awakeFromNib()'时,所有内容都是相同的 –

回答

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 
} 

}