gpt4 book ai didi

xcode - 具有图层背景颜色的 OSX NSTextField 不起作用

转载 作者:行者123 更新时间:2023-12-03 16:40:27 27 4
gpt4 key购买 nike

我有以下 NSTextField 子类(具有用于间距的 NSTextFieldCell 相应子类 [approach found 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

最佳答案

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

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

去掉单元格中所有自定义的东西。苹果希望尽快淘汰它们......甚至不确定是否有任何有效的手机壳可以与电池一起使用:

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 {

}

这是我得到的图片: enter image description here

关于xcode - 具有图层背景颜色的 OSX NSTextField 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com