Ron*_*ann 31 macos objective-c textcolor nsbutton
在OSX上我有一个非常暗的图像的NSButton,不幸的是,使用属性检查器无法更改颜色.看图片大黑色按钮,文字是Go.
是否有可能改变文字颜色的线索?我查看了NSButton类,但没有方法可以做到这一点.我知道我可以使用白色字体制作图像,但这不是我想要做的.
来自瑞士的问候,罗纳德霍夫曼---
小智 47
这是另外两个解决方案: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];
}
Run Code Online (Sandbox Code Playgroud)
解决方案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];
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*der 14
我的解决方案
.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
Run Code Online (Sandbox Code Playgroud)
这是一个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)
}
}
Run Code Online (Sandbox Code Playgroud)
和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)
}
}
Run Code Online (Sandbox Code Playgroud)
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];
Run Code Online (Sandbox Code Playgroud)
请注意,调用fixAttributesInRange:
似乎很重要(AppKit扩展),但我找不到文档为何会出现这种情况.我在NSButton中使用属性字符串的唯一问题是,如果还为按钮定义了图像(例如图标),则属性字符串将占据一个大矩形并将图像推到按钮的边缘.要记住一些事情.
否则,最好的办法似乎是制作你自己的drawRect:
覆盖,这有许多其他缺陷超出了这个问题的范围.
我创建了一个NSButton
名为 的子类FlatButton
,它使得在 Interface Builder 的属性检查器中更改文本颜色变得非常容易,就像您所要求的那样。它应该为您的问题提供简单而广泛的解决方案。
它还公开其他相关的样式属性,例如颜色和形状。
您可以在这里找到它: https: //github.com/OskarGroth/FlatButton
这就是我在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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30185 次 |
最近记录: |