OSX NSTextField与图层背景颜色不工作 [英] OSX NSTextField with layer background color not working

查看:463
本文介绍了OSX NSTextField与图层背景颜色不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下子类 NSTextField (对应的子类 NSTextFieldCell =http://stackoverflow.com/a/9027461/895453>这里])。我尝试更改焦点上的边框颜色和背景颜色,但它不工作。

I have the following subclass of NSTextField (with corresponding subclass of NSTextFieldCell for spacing [approach found here]). I'm trying to change the border color and background color on "focus", but it isn't working.

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)
    }
}

事实上, c $ c> self.layer?.backgroundColor 到任何地方的任何地方都不工作。我不能只是在文本字段本身设置 backgroundColor ,因为基于单元格,背景颜色被抵消。

In fact, it appears that trying to set self.layer?.backgroundColor to anything anywhere doesn't work. I can't just set backgroundColor on the textfield itself, because based on the cell, the background color is offset.

如何在 NSTextField 子类的层上设置 backgroundColor

How can I set the backgroundColor on the layer of an NSTextField subclass?

推荐答案

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

Looks like a simple problem... but requires a lot of hacky code. I have some working snippet for you.

确保你的文本字段是层支持的!

Make sure your text fields are layer backed!

所有这些定制的东西在单元格。苹果想尽快退休...没有确定任何有效的情况下操作单元格:

Get rid of all that custom stuff in the cell. Apple want to retire them soon... Not even sure any valid cases operate with cells:

class TGTextFieldCell: NSTextFieldCell {

}

接下来我们得到实际:

class TGTextField: NSTextField

定义属性控制焦点状态:

Define a property holding focused state:

private var isFocused = false

覆盖成为第一个响应者,如同最初一样:

Override becoming first responder as you did originally:

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

不幸的是,我们不能轻易捕捉到模糊的瞬间。在互联网上有一些长的解释...但我们将捕获模糊的事件订阅:

Unfortunately, we cannot capture the moment of blur easily. There are some long explanations for that in internet... but we will capture blur in a the event subscription:

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

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

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

Now call that subscription in the constructors overrides:

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

    subscribeForEndEdit()
}

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

    subscribeForEndEdit()
}

    self.drawsBackground = false

最后一个是改变绘图背景:

The final piece is changing background on drawing:

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 {

}

这是我得到的图片:

Here is the picture of what I get:

这篇关于OSX NSTextField与图层背景颜色不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆