我使用以下代码对 NSTextField 进行了子类化:
import Cocoa
class CustomSearchField: NSTextField {
override func draw(_ dirtyRect: NSRect) {
self.wantsLayer = true
let textFieldLayer = CALayer()
self.layer = textFieldLayer
self.backgroundColor = NSColor.white
self.layer?.backgroundColor = CGColor.white
self.layer?.borderColor = CGColor.white
self.layer?.borderWidth = 0
super.cell?.draw(withFrame: dirtyRect, in: self)
}
}
class CustomSearchFieldCell: NSTextFieldCell {
override func drawingRect(forBounds rect: NSRect) -> NSRect {
let minimumHeight = self.cellSize(forBounds: rect).height
let newRect = NSRect(x: rect.origin.x + 25, y: (rect.origin.y + (rect.height - minimumHeight) / 2) - 4, width: rect.size.width - 50, height: minimumHeight)
return super.drawingRect(forBounds: newRect)
}
}
一切正常,它按照我的意愿绘制了我的 NSTextField。唯一的问题是,一旦我将界面的其他部分设置为第一响应者(在 NSTextField 外部单击),NSTextField 内的文本(占位符或填充文本)就会淡出。一旦我再次点击它,它就会淡出。我一直在寻找安静的时间,但无法真正弄清楚为什么会这样。我只希望文本始终可见,而不是淡入淡出。
它与我为完成我的样式而添加的 CALayer 有关。
每当我在文本字段上从 viewDidLoad 运行相同的设置时,它就像一个魅力。例如:
class ViewController: NSViewController {
@IBOutlet weak var searchField: NSTextField!
override func viewDidLoad() {
initCustomSearchField()
}
private func initCustomSearchField() {
searchField.wantsLayer = true
let textFieldLayer = CALayer()
searchField.layer = textFieldLayer
searchField.backgroundColor = NSColor.white
searchField.layer?.backgroundColor = CGColor.white
searchField.layer?.borderColor = CGColor.white
searchField.layer?.borderWidth = 0
searchField.delegate = self
}
}
最佳答案
draw 方法应该真正用于绘制 View 而不是设置属性。对于你的问题,不要设置为 self.layer 直接使用 sublayer 代替。
我对您的代码的建议:
class CustomTextField :NSTextField {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
func setupView(){
textColor = .green
}
}
class CustomTextFieldCell: NSTextFieldCell {
override init(textCell string: String) {
super.init(textCell: string)
setupView()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
func setupView(){
backgroundColor = .red
}
override func drawingRect(forBounds rect: NSRect) -> NSRect {
let newRect = NSRect(x: (rect.width - rect.width/2)/2, y: 0, width: rect.width/2, height: 20)
return super.drawingRect(forBounds:newRect)
}
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
super.draw(withFrame: cellFrame, in: controlView)
controlView.layer?.borderColor = NSColor.white.cgColor
controlView.layer?.borderWidth = 2
}
}
关于swift - NSTextField 在子类化后淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292681/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案
我可以访问ruby的异常层次结构(它在镐和蜂鸟中都提到过),但我不确定使用哪个异常,因为我没有找到关于每个术语含义的任何信息。使用正确的异常类重要吗? 最佳答案 创建您自己的异常时很重要。一个重要的警告是,继承自Exception而不是StandardError(常见错误)的异常不会被rescue捕获(没有任何参数)。 关于ruby-在ruby中使用正确的异常子类,我们在StackOverflow上找到一个类似的问题: https://stackove
我希望为日志设置一个默认路径,相对于使用日志的文件路径,像这样:#/path/to/lib/bar.rbclassBardefsettings_file_pathFile.dirname(File.expand_path(__FILE__))endend#/path/to/app/models/foo.rbclassFoo理想输出:#=>/path/to/app/models实际输出:#=>/path/to/lib因为FILE引用了它写入的文件,而不是它被调用的地方,它返回bar.rb文件,但我想要这样的东西返回foo.rb文件的路径,即使该方法是在Bar中定义的。有人有什么建议吗?
我的第一个想法是这样的:classAbstractBuilderattr_reader:time_takendefbuild_with_timerstarted_at=Time.nowbuild@time_taken=Time.now-started_atenddefbuildraise'Implementthismethodinasubclass'endendclassMyBuilder我怀疑有更好的方法可以提供更好的灵active,例如理想情况下,我想在MyBuilder的实例上调用“build”而不是“build_with_timer”,并且始终记录执行时间。我确实考虑过使用al
我的项目中有一个类别和子类别模型。我想以灵活的方式拥有许多子级别。我想制作一个self引用的“父”外键,但我不太确定该怎么做。有任何想法吗?谢谢!Cat1Sub1SubSub1SubSub2Sub2Cat2Sub1Cat3Sub1Sub2SubSub1 最佳答案 试试acts_as_tree插件 关于ruby-on-rails-在Rails中实现具有灵活深度的类别和子类别的最佳方法?,我们在StackOverflow上找到一个类似的问题: https://st
所以我知道您不应该直接继承Fixnum、Float或Integer,因为它们没有#new方法。虽然使用DelegateClass似乎可行,但这是最好的方法吗?任何人都知道这些类没有#new的原因是什么?我需要一个行为类似于Fixnum的类,但有一些额外的方法,我希望能够从类中通过self引用它的值,例如:classFoo10 最佳答案 您可以很容易地自己设置一个快速转发实现:classMyNumdefinitialize(number)@number=numberenddefmethod_missing(name,*args,&bl
我正在开发一个电子商务应用程序,试图解决以下问题:我通过awesome_nested_set插件实现了我的类别。如果我通过选择一个类别列出我的文章,一切正常,但对于某些链接,我想显示一个类别的所有产品及其子类别的产品。这里是仅适用于一个类别的Controller代码:#products_controller.rbdefindexifparams[:category]@category=Category.find(params[:category])#@products=@category.product_list@products=@category.productselse@cate
像下面这样在其父类下定义子类是惯例吗?classElementclassDiv还是让模块包含子类更合适?classElementendmoduleElementsclassDiv或者在一个模块中创建一个“基”类并在同一个模块中定义子类?moduleElementclassBaseendclassDiv或者强制命名约定更好?classElementendclassDivElement似乎每个库都选择不同的命名空间/命名约定。哪个最好用?各自的优缺点是什么? 最佳答案 TL;博士:最传统和最好的方法是使用包含基类及其子类的模块。但让我们