OSX 图层背景颜色的 NSTextField 不工作

OSX NSTextField with layer background color not working

我有以下 NSTextField 的子类(NSTextFieldCell 的相应子类用于间距 [找到方法 here])。我正在尝试更改 "focus" 上的边框颜色和背景颜色,但它不起作用。

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

尝试将您设置的背景颜色代码放入viewWillDraw。例如:

-(void)viewWillDraw {
    [super setBackgroundColor:[NSColor yellowColor]];
}

看起来是个简单的问题...但需要大量复杂的代码。我有一些工作片段给你。

确保您的文本字段有图层支持!

删除单元格中的所有自定义内容。苹果希望尽快淘汰它们......甚至不确定是否有任何有效案例适用于电池:

class TGTextFieldCell: NSTextFieldCell {

}

接下来我们进入实际:

class TGTextField: NSTextField

定义一个属性保持专注状态:

private var isFocused = false

像您原来那样重写成为第一响应者:

override func becomeFirstResponder() -> Bool {
    if super.becomeFirstResponder() {
        isFocused = true
        return true
    } else {
        return false
    }
}

遗憾的是,我们无法轻易捕捉到模糊的瞬间。在互联网上对此有一些很长的解释......但我们将在事件订阅中捕获模糊:

    func subscribeForEndEdit(){
    self.drawsBackground = false
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"didEndEdit:", name: NSControlTextDidEndEditingNotification, object: nil)
}

func didEndEdit(notification: NSNotification){
    isFocused = false
}

现在在构造函数中调用该订阅覆盖:

override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)

    subscribeForEndEdit()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)

    subscribeForEndEdit()
}

现在防止绘制背景:

    self.drawsBackground = false

最后一块正在改变绘图的背景:

override func drawRect(dirtyRect: NSRect) {
    //Draw cell content here
    super.cell?.drawInteriorWithFrame(dirtyRect, inView: self)

    if isFocused {

        self.layer?.backgroundColor = NSColor.redColor().CGColor
    } else {

        self.layer?.backgroundColor = NSColor.whiteColor().CGColor
    }


}

整个文件是:

import Cocoa

class TGTextField: NSTextField {

private var isFocused = false

override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)

    subscribeForEndEdit()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)

    subscribeForEndEdit()
}

  func subscribeForEndEdit(){
    self.drawsBackground = false
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"didEndEdit:", name: NSControlTextDidEndEditingNotification, object: nil)
}

func didEndEdit(notification: NSNotification){
    isFocused = false
}


override func drawRect(dirtyRect: NSRect) {
    //Draw cell content here
    super.cell?.drawInteriorWithFrame(dirtyRect, inView: self)

    if isFocused {

        self.layer?.backgroundColor = NSColor.redColor().CGColor
    } else {

        self.layer?.backgroundColor = NSColor.whiteColor().CGColor
    }


}

override func becomeFirstResponder() -> Bool {
    if super.becomeFirstResponder() {
        isFocused = true
        return true
    } else {
        return false
    }
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

}

class TGTextFieldCell: NSTextFieldCell {

}

这是我得到的图片: