objective-c - 在 NSButtonCell 上使用 NSBackgroundStyleLowered?

标签 objective-c xcode macos cocoa

我使用这段代码...

[[textField cell] setBackgroundStyle:NSBackgroundStyleLowered];

...给一段文本添加阴影,并且它有效。当我尝试使用按钮执行相同的操作时:

[[refreshButton cell] setBackgroundStyle:NSBackgroundStyleLowered];

该代码不起作用。该按钮是一个带有白色透明圆形箭头的瞬时更改按钮。有什么想法为什么这行不通吗?看起来它会起作用,因为它仍然是一个细胞。

最佳答案

NSCell 子类具有不同的绘制行为。因此,可设置的背景样式并不意味着该样式实际上在具体子类中使用。

NSButtonCells 使用interiorBackgroundStyle绘制标题之前的属性。此属性不公开 setter ,因此您必须继承 NSButtonCell 并相应地在 Interface Builder 中设置单元格类。
要实现降低的背景样式,请在子类中重写 InteriorBackgroundStyle:

- (NSBackgroundStyle)interiorBackgroundStyle
{
    return NSBackgroundStyleLowered;
}

如果您需要对绘图进行更多控制,您还可以覆盖 NSButtonCell 的 drawInteriorWithFrame:inView: .

一种黑客方法(不需要子类化)是修改属性标题字符串以达到类似的效果:

NSShadow* shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSAttributedString* title = [button.cell attributedTitle];
NSMutableDictionary* attributes = [[title attributesAtIndex:0 longestEffectiveRange:NULL inRange:NSMakeRange(0, title.length)] mutableCopy];
[attributes setObject:shadow forKey:NSShadowAttributeName];
NSAttributedString* string = [[NSAttributedString alloc] initWithString:[button.cell title] attributes:attributes];
[button.cell setAttributedTitle:string];

关于objective-c - 在 NSButtonCell 上使用 NSBackgroundStyleLowered?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758656/

相关文章:

objective-c - init] 在自动引用计数中

iphone - 看不出为什么这里有内存泄漏

iOS - 当定位服务被接受时叫什么?

swift - ObjectMapper 嵌套(三重)字典

ios - 从 xib 文件动态加载 iOS View

ios - Sources 文件夹中的文件无法访问 Xcode Playground 中其他文件的公共(public)类或函数

c++ - Qt 4.7 - 工具提示和 QComboBox 弹出窗口在 OS X 10.12 下出现空白

ios - UICollectionView RTL 方向

objective-c - iOS相机编程-如何快速获取静止图像使用AVCaptureVideoDataOutput和AVCaptureStillImageOutput案例

Eclipse 无法调试。请帮助我尝试了一切