在NSButton中绘制NSString时绘制工件

时间:2015-02-02 13:39:28

标签: cocoa nsstring drawrect nsimage nsbutton

我正在使用drawRect在自定义视图中绘制NSString,该视图是NSButton的子视图。 NSButton的drawRect也在绘制NSImage,这似乎是问题所在。如果我删除drawRect的{​​{1}}代码,则不再发生(但之后我没有背景)。

以下是自定义视图NSImage(缩写):

drawRect

以下是override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) let string: NSString = NSString(string: thermostat.displayTargetTemperature()) string.drawAtPoint(NSMakePoint(CGFloat(width), CGFloat(y)), withAttributes: self.attributes) } NSButton

drawRect

绘制override func drawRect(dirtyRect: NSRect) { var image = NSImage(named: FILDarkManager.enabled ? "preferences-statusbar-bg-dark" : "preferences-statusbar-bg-light") if (self.state == NSOnState) { image?.drawInRect(dirtyRect, fromRect: NSZeroRect, operation: NSCompositingOperation.CompositeSourceOver, fraction: 1.0, respectFlipped: true, hints: nil) } else { image?.drawInRect(dirtyRect, fromRect: NSZeroRect, operation: NSCompositingOperation.CompositeSourceOver, fraction: 0.8, respectFlipped: true, hints: nil) } super.drawRect(dirtyRect) } 时可能与操作有关吗?

奇怪的是,如果我将鼠标悬停在视图上,它会正确绘制。

Screenshot

1 个答案:

答案 0 :(得分:1)

尝试将图像绘制到[self bounds]而不是dirtyRect。

绘制字符串可能会弄脏按钮的一部分,导致代码将图像绘制到dirtyRect中。