2017-06-06 95 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 
} 

}