我使用这段代码...
[[textField cell] setBackgroundStyle:NSBackgroundStyleLowered];...to给一段文本加上阴影,它就能起作用。当我尝试用按钮做同样的事情时:
[[refreshButton cell] setBackgroundStyle:NSBackgroundStyleLowered];代码不起作用。该按钮是一个带有白色透明圆形箭头的瞬间更改按钮。你知道为什么这个不能工作吗?它似乎可以工作,因为它仍然是一个细胞。
发布于 2013-04-03 16:51:59
NSCell子类具有不同的绘制行为。因此,一个可设置的背景样式并不意味着该样式实际上被用于具体的子类。
NSButtonCells在绘制标题之前使用interiorBackgroundStyle属性。此属性不公开设置器,因此您必须继承NSButtonCell的子类,并在接口生成器中相应地设置单元格类。
要实现lowered背景样式,请重写子类中的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];https://stackoverflow.com/questions/15758656
复制相似问题