macos - 如何绘制 NSPopupButtonCell 突出显示的图像
问题描述
我在里面有 NSPopupButton NSToolbar
,borderder = false
它在突出显示时有一些颜色混合。如果我使用bordered = true
图像是用漂亮的深色叠加绘制的。
我正在尝试以与它相同的方式绘制突出显示的状态bordered=true
PS:NSButtonCellbordered = false
开箱即用。
bordered = true
我可以通过拥有和覆盖来实现这种行为,drawBezel
并且在那里什么都不做,但我想知道
我尝试过的事情:
- 亮点通过
- 室内背景风格
- 设置细胞属性
-
class ToolbarPopUpButtonCell : NSPopUpButtonCell {
override var isHighlighted: Bool {
get { return true }
set { super.isHighlighted = newValue }
}
override func drawImage(withFrame cellFrame: NSRect, in controlView: NSView) {
super.drawImage(withFrame: cellFrame, in: controlView)
}
//used in case bordered = true so we do nothing
override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
}
//doesn't work
override var interiorBackgroundStyle: NSView.BackgroundStyle
{
return .raised
}
}
class ToolbarPopUpButton: NSPopUpButton {
override func awakeFromNib() {
cell?.setCellAttribute(.cellLightsByBackground, to: 1)
}
override var intrinsicContentSize: NSSize {
return NSMakeSize(32 + 5, 32)
}
}
注意右边的图像适用于bordered = false
( NSButtonCell
)
解决方案
我发现的唯一技巧是绘制一个替代单元格。仍在寻求更好的解决方案。
- (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSImage *image = [self image];
NSButtonCell *swapCell = [[NSButtonCell alloc] initImageCell:image];
[swapCell setHighlighted:[self isHighlighted]];
[swapCell drawImage:image withFrame:cellFrame inView:controlView];
}
推荐阅读
- python - 使用 pandas 的泊松过程的 KeyError
- android - 在 Android 中实现语言选择器
- c# - 如何确保从抽象泛型类派生的类将自身用作泛型参数
- c# - 如何使用 powershell 从非提升的 .NET 应用程序预配 UWP 应用程序
- excel - 在 Pandas 中转置数据
- spring-integration - 如何在spring集成数据库轮询器中从多个表中获取数据
- ios - SWIFT:navigationviewcontroller 不会将应用程序转换到另一个视图控制器
- regex - 一次匹配 2 个模式的正则表达式或匹配两者的 Applescript 中的 SED
- reactjs - InAppBrowser 未在 Ionic React App 中打开
- ansible - Ansible Jinja2 模板循环迭代