Objective c NSButton如何为文本着色
在OSX上,我有一个NSButton,图像非常暗,不幸的是,无法使用属性检查器更改颜色。请参见图片中的黑色大按钮,文本为Go 有没有可能改变文本颜色的线索?我查看了NSButton类,但是没有方法可以做到这一点。我知道我可以用白色字体制作图像,但这不是我想做的 来自瑞士的问候,罗纳德·霍夫曼Objective c NSButton如何为文本着色,objective-c,macos,nsbutton,textcolor,Objective C,Macos,Nsbutton,Textcolor,在OSX上,我有一个NSButton,图像非常暗,不幸的是,无法使用属性检查器更改颜色。请参见图片中的黑色大按钮,文本为Go 有没有可能改变文本颜色的线索?我查看了NSButton类,但是没有方法可以做到这一点。我知道我可以用白色字体制作图像,但这不是我想做的 来自瑞士的问候,罗纳德·霍夫曼 ---这里还有另外两种解决方案: 解决方案1: -(void)awakeFromNib { NSColor *color = [NSColor greenColor]; NSMutabl
---这里还有另外两种解决方案: 解决方案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];
}
苹果公司拥有设置NSButton文本颜色的代码,该代码是应用程序的一部分 下面是该示例的关键(本篇文章稍作修改,未经测试): 请注意,对
fixAttributesInRange:
的调用似乎很重要(AppKit扩展),但我找不到关于为什么会这样的文档。在NSButton中使用属性字符串的唯一问题是,如果还为按钮定义了图像(例如图标),则属性字符串将占据一个大矩形,并将图像推到按钮的边缘。要记住的事情
否则,最好的方法似乎是让您自己的drawRect:
替代,这有许多其他陷阱,超出了这个问题的范围。我的解决方案:
.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)
}
}
在NSButton上添加一个类别,只需将颜色设置为所需的颜色,并保存现有属性,因为标题可以居中、左对齐等
一个真正简单、可重用的解决方案,无需子类化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];
}
我已经创建了一个名为
FlatButton
的NSButton
子类,它可以非常轻松地更改Interface Builder属性检查器中的文本颜色,就像您所要求的那样。它应该为您的问题提供简单而广泛的解决方案
它还公开其他相关的样式属性,例如颜色和形状
您可以在这里找到它:
这就是我在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
使用上面的信息,我编写了一个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
}
}
当您的目标是macOS 10.14或更新版本时,您可以使用NSButton控件的new
contentTintColor
属性来设置文本颜色
button.contentTintColor = NSColor.systemGreenColor;
David Boyd解决方案的Swift 4.2版本
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
}
}Swift 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])
}
}
}
现在,您可以轻松设置故事板中的标题颜色。干杯 如果部署目标>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
不,还没有,等一下:)同样的事情NSAttributedString中没有setColor。
-[NSMutableAttributedString addAttribute:NSForegroundColorAttributeName值:[NSColorWithRGred:1.0绿色:0.0蓝色:0.0 alpha:0.0]范围:NSMakeRange(0,string.length)]代码>非常好的H2CO3,感谢您的努力。Go现在是白色的。但现在围棋在左边的按钮外面是白色的,很小。很遗憾,无法在此处粘贴图像;(我选择了Position,但没有成功。我不明白为什么字体大小和位置会发生变化?对吗?我假设会和以前一样。我只是想改变字体颜色。这并不是你遇到的问题的答案,但为什么不在你初始化按钮的图像中添加任何你想要的文本呢?)st输入…是的,这绝对是一个解决方案。另一方面,我很好奇,想知道事情是如何运作的。请编辑你的答案并格式化代码,使其可读。请正确格式化。这样,其他用户更容易阅读你的帖子。点击按钮文本被删除,osx 10.11,xcode 8.1你能添加一些文本来解释吗您的解决方案?这是Xamarin.Mac的答案。它对我很有帮助。我建议将属性字符串基于button.AttributedTitle而不是button.Cell.Title,这样可以保留标题所具有的任何其他(隐式)属性。您认为可以为此解决方案添加圆角吗?:@Yatko只需替换NSRectFill(dirtyRect)使用:let rect=NSMakeRect(0,0,bounds.width,bounds.height)let rectanglePath=NSBezierPath(roundedRect:rect,xRadius:3,yRadius:3)rectanglePath.fill()contentTintColor
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])
}
}
}
/** 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