前阵子做项目时,界面button需要贴图,但是美工给的背景图片太小无法直接贴上去。考虑过让美工重新做图,但是考虑到要做多语言的话,那美工需要做的图片就相当多了。因此我决定重写button,将背景图直接拉伸之后绘制上去。
这就涉及到一个问题,背景图颜色比较深,绘制完之后button的title看不清楚,影响用户体验,可是系统又没有提供直接设置button字体颜色的选项。这时我就在考虑,能不能通过其他方式修改button的字体颜色。后面终于在google上找到了解决方法:
- (void)setTextColor:(NSButton *)button color:(NSColor *)textColor
{
NSMutableAttributedString *attrTitle = [[NSMutableAttributedStringalloc] initWithAttributedString:[buttonattributedTitle]];
NSUInteger len = [attrTitle length];
NSRange range = NSMakeRange(0, len);
[attrTitle addAttribute:NSForegroundColorAttributeNamevalue:textColor range:range];
[attrTitle fixAttributesInRange:range];
[button setAttributedTitle:attrTitle];
[attrTitle release]; attrTitle = nil;
[button setNeedsDisplay:YES];
}
调用
[selfsetTextColor:_btnBegin color:[NSColorgreenColor]];,传入需要设置的button,设置字体为绿色,运行。。。