【发布时间】:2016-10-18 15:17:05
【问题描述】:
我有以下NSTextField 的子类(对应的子类NSTextFieldCell 用于间距[发现方法here])。我正在尝试更改“焦点”上的边框颜色和背景颜色,但它不起作用。
class TGTextField: NSTextField {
override func viewWillDraw() {
self.layer?.borderWidth = 1
self.layer?.cornerRadius = 2
self.textColor = NSColor(calibratedRed: 55/255, green: 54/255, blue: 54/255, alpha: 1)
self.focusRingType = .None
self.font = NSFont(name: "ProximaNova-Medium", size: 16)!
setBlurredStyles()
}
private func setFocusedStyles() {
self.layer?.borderColor = NSColor(calibratedRed: 92/255, green: 188/255, blue: 214/255, alpha: 1).CGColor
self.layer?.backgroundColor = NSColor(calibratedRed: 247/255, green: 252/255, blue: 252/255, alpha: 1).CGColor
}
private func setBlurredStyles() {
self.layer?.borderColor = NSColor(calibratedRed: 221/255, green: 221/255, blue: 221/255, alpha: 1).CGColor
self.layer?.backgroundColor = NSColor.whiteColor().CGColor
}
override func becomeFirstResponder() -> Bool {
if super.becomeFirstResponder() {
dispatch_async(dispatch_get_main_queue(), {
self.setFocusedStyles()
})
return true
} else {
return false
}
}
override func resignFirstResponder() -> Bool {
if super.resignFirstResponder() {
dispatch_async(dispatch_get_main_queue(), {
self.setBlurredStyles()
})
return true
} else {
return false
}
}
}
class TGTextFieldCell: NSTextFieldCell {
override init(imageCell image: NSImage?) {
super.init(imageCell: image)
}
override init(textCell aString: String) {
super.init(textCell: aString)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawingRectForBounds(theRect: NSRect) -> NSRect {
let rectInset = NSMakeRect(theRect.origin.x + 7, theRect.origin.y + 7, theRect.size.width, theRect.size.height)
return super.drawingRectForBounds(rectInset)
}
}
事实上,尝试将self.layer?.backgroundColor 设置为任何地方的任何东西似乎都行不通。我不能只在文本字段本身上设置backgroundColor,因为根据单元格,背景颜色是偏移的。
如何在NSTextField 子类的层上设置backgroundColor?
【问题讨论】: