在 OSX 上,我有一个带有非常暗图像的 NSButton,不幸的是,无法使用属性检查器更改颜色。看图片黑色的大按钮,文字是Go。
是否有可能更改文本颜色的线索?我查看了 NSButton 类,但没有方法可以做到这一点。我知道我可以用白色字体制作图像,但这不是我想做的。
来自瑞士的问候,罗纳德·霍夫曼——
在 OSX 上,我有一个带有非常暗图像的 NSButton,不幸的是,无法使用属性检查器更改颜色。看图片黑色的大按钮,文字是Go。
是否有可能更改文本颜色的线索?我查看了 NSButton 类,但没有方法可以做到这一点。我知道我可以用白色字体制作图像,但这不是我想做的。
来自瑞士的问候,罗纳德·霍夫曼——
这是另外两个解决方案:
http: //denis-druz.okis.ru/news.534557.Text-Color-in-NSButton.html
解决方案1:
-(void)awakeFromNib
{
NSColor *color = [NSColor greenColor];
NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
NSRange titleRange = NSMakeRange(0, [colorTitle length]);
[colorTitle addAttribute:NSForegroundColorAttributeName value:color range:titleRange];
[button setAttributedTitle:colorTitle];
}
解决方案2:
在 *.m 文件中:
- (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title withColor:(NSColor*)color
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSCenterTextAlignment];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:title attributes:attrsDictionary];
[button setAttributedTitle:attrString];
}
-(void)awakeFromNib
{
NSString *title = @"+Add page";
NSColor *color = [NSColor greenColor];
[self setButtonTitleFor:button toString:title withColor:color];
}
我的解决方案:
.h
IB_DESIGNABLE
@interface DVButton : NSButton
@property (nonatomic, strong) IBInspectable NSColor *BGColor;
@property (nonatomic, strong) IBInspectable NSColor *TextColor;
@end
.m
@implementation DVButton
- (void)awakeFromNib
{
if (self.TextColor)
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSCenterTextAlignment];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.TextColor, NSForegroundColorAttributeName,
self.font, NSFontAttributeName,
style, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:self.title attributes:attrsDictionary];
[self setAttributedTitle:attrString];
}
}
- (void)drawRect:(NSRect)dirtyRect
{
if (self.BGColor)
{
// add a background colour
[self.BGColor setFill];
NSRectFill(dirtyRect);
}
[super drawRect:dirtyRect];
}
@end
这是一个 Swift 3 版本:
import Cocoa
@IBDesignable
class DVButton: NSButton
{
@IBInspectable var bgColor: NSColor?
@IBInspectable var textColor: NSColor?
override func awakeFromNib()
{
if let textColor = textColor, let font = font
{
let style = NSMutableParagraphStyle()
style.alignment = .center
let attributes =
[
NSForegroundColorAttributeName: textColor,
NSFontAttributeName: font,
NSParagraphStyleAttributeName: style
] as [String : Any]
let attributedTitle = NSAttributedString(string: title, attributes: attributes)
self.attributedTitle = attributedTitle
}
}
override func draw(_ dirtyRect: NSRect)
{
if let bgColor = bgColor
{
bgColor.setFill()
NSRectFill(dirtyRect)
}
super.draw(dirtyRect)
}
}
和 Swift 4.0 版本:
import Cocoa
@IBDesignable
class Button: NSButton
{
@IBInspectable var bgColor: NSColor?
@IBInspectable var textColor: NSColor?
override func awakeFromNib()
{
if let textColor = textColor, let font = font
{
let style = NSMutableParagraphStyle()
style.alignment = .center
let attributes =
[
NSAttributedStringKey.foregroundColor: textColor,
NSAttributedStringKey.font: font,
NSAttributedStringKey.paragraphStyle: style
] as [NSAttributedStringKey : Any]
let attributedTitle = NSAttributedString(string: title, attributes: attributes)
self.attributedTitle = attributedTitle
}
}
override func draw(_ dirtyRect: NSRect)
{
if let bgColor = bgColor
{
bgColor.setFill()
__NSRectFill(dirtyRect)
}
super.draw(dirtyRect)
}
}
这就是我在 Swift 4 中完成它的方式
@IBOutlet weak var myButton: NSButton!
// create the attributed string
let myString = "My Button Title"
let myAttribute = [ NSAttributedStringKey.foregroundColor: NSColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
// assign it to the button
myButton.attributedTitle = myAttrString
Apple 有用于设置 NSButton 的文本颜色的代码作为Popover 示例的一部分。
下面是示例的关键(针对这篇文章稍作修改,未经测试):
NSButton *button = ...;
NSMutableAttributedString *attrTitle =
[[NSMutableAttributedString alloc] initWithString:@"Make Me Red"];
NSUInteger len = [attrTitle length];
NSRange range = NSMakeRange(0, len);
[attrTitle addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:range];
[attrTitle fixAttributesInRange:range];
[button setAttributedTitle:attrTitle];
请注意,调用fixAttributesInRange:
似乎很重要(AppKit 扩展),但我找不到关于为什么会这样的文档。我对在 NSButton 中使用属性字符串的唯一担忧是,如果还为按钮定义了图像(例如图标),属性字符串将占据一个大矩形并将图像推到按钮的边缘。有什么要记住的。
否则,最好的方法似乎是自己进行drawRect:
覆盖,这有许多其他陷阱超出了这个问题的范围。
我创建了一个NSButton
名为的子类FlatButton
,它可以像您要求的那样非常容易地在 Interface Builder 的属性检查器中更改文本颜色。它应该为您的问题提供简单而广泛的解决方案。
它还公开了其他相关的样式属性,例如颜色和形状。
当您的目标是 macOS 10.14 或更高版本时,您可以使用contentTintColor
NSButton 控件的新属性来设置文本颜色。
button.contentTintColor = NSColor.systemGreenColor;
在 NSButton 上添加一个类别,只需将颜色设置为您想要的颜色,并保留现有属性,因为标题可以居中、左对齐等
@implementation NSButton (NSButton_IDDAppKit)
- (NSColor*)titleTextColor {
return [NSColor redColor];
}
- (void)setTitleTextColor:(NSColor*)aColor {
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedTitle];
NSString* title = self.title;
NSRange range = NSMakeRange(0.0, self.title.length);
[attributedString addAttribute:NSForegroundColorAttributeName value:aColor range:range];
[self setAttributedTitle:attributedString];
[attributedString release];
}
@end
一个非常简单、可重用的解决方案,无需子类化 NSButton:
[self setButton:self.myButton fontColor:[NSColor whiteColor]] ;
-(void) setButton:(NSButton *)button fontColor:(NSColor *)color {
NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
[colorTitle addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, button.attributedTitle.length)];
[button setAttributedTitle:colorTitle];
}
如果部署目标> 10.14,您可以使用contentTintColor
设置颜色。
/** Applies a tint color to template image and text content, in combination with other theme-appropriate effects. Only applicable to borderless buttons. A nil value indicates the standard set of effects without color modification. The default value is nil. Non-template images and attributed string values are not affected by the contentTintColor. */
@available(macOS 10.14, *)
@NSCopying open var contentTintColor: NSColor?
示例代码:
var confirmBtn = NSButton()
confirmBtn.title = "color"
confirmBtn.contentTintColor = NSColor.red
斯威夫特 5
你可以使用这个扩展:
extension NSButton {
@IBInspectable var titleColor: NSColor? {
get {
return NSColor.white
}
set {
let pstyle = NSMutableParagraphStyle()
pstyle.alignment = .center
self.attributedTitle = NSAttributedString(
string: self.title,
attributes: [ NSAttributedString.Key.foregroundColor :newValue, NSAttributedString.Key.paragraphStyle: pstyle])
}
}
}
现在您可以轻松地在情节提要中设置标题颜色。干杯!
NSColor color = NSColor.White;
NSMutableAttributedString colorTitle = new NSMutableAttributedString (cb.Cell.Title);
NSRange titleRange = new NSRange (0, (nint)cb.Cell.Title.Length);
colorTitle.AddAttribute (NSStringAttributeKey.ForegroundColor, color, titleRange);
cb.Cell.AttributedTitle = colorTitle;
使用上面的信息,我编写了一个 NSButton 扩展来设置前景色,以及系统字体和文本对齐方式。
这适用于 Swift 4.x 上的 Cocoa,但可以很容易地针对 iOS 进行调整。
import Cocoa
extension NSButton {
func setAttributes(foreground: NSColor? = nil, fontSize: CGFloat = -1.0, alignment: NSTextAlignment? = nil) {
var attributes: [NSAttributedStringKey: Any] = [:]
if let foreground = foreground {
attributes[NSAttributedStringKey.foregroundColor] = foreground
}
if fontSize != -1 {
attributes[NSAttributedStringKey.font] = NSFont.systemFont(ofSize: fontSize)
}
if let alignment = alignment {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
attributes[NSAttributedStringKey.paragraphStyle] = paragraph
}
let attributed = NSAttributedString(string: self.title, attributes: attributes)
self.attributedTitle = attributed
}
}
Swift 4.2 版本 David Boyd 的解决方案
extension NSButton {
func setAttributes(foreground: NSColor? = nil, fontSize: CGFloat = -1.0, alignment: NSTextAlignment? = nil) {
var attributes: [NSAttributedString.Key: Any] = [:]
if let foreground = foreground {
attributes[NSAttributedString.Key.foregroundColor] = foreground
}
if fontSize != -1 {
attributes[NSAttributedString.Key.font] = NSFont.systemFont(ofSize: fontSize)
}
if let alignment = alignment {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
attributes[NSAttributedString.Key.paragraphStyle] = paragraph
}
let attributed = NSAttributedString(string: self.title, attributes: attributes)
self.attributedTitle = attributed
}
}