如何设置NSButtonCell文本标签的颜色

feb*_*ing 5 macos cocoa objective-c

如何设置NSButtonCell标签(标题)文本的颜色,即表格视图的列单元格?就我而言,它是一个复选框按钮单元格.(有可能使用IB吗?)

Muh*_*wan 12

尝试这个,我认为它是完美的.

NSColor *color = [NSColor redColor];
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)


Pie*_*ard 9

您可以尝试使用属性字符串值.

NSColor *txtColor = [NSColor redColor];
NSFont *txtFont = [NSFont boldSystemFontOfSize:14];
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys:
        txtFont, NSFontAttributeName, txtColor, NSForegroundColorAttributeName, nil];
NSAttributedString *attrStr = [[[NSAttributedString alloc]
        initWithString:@"Hello!" attributes:txtDict] autorelease];
[[attrStrTextField cell] setAttributedStringValue:attrStr];
[attrStrTextField updateCell:[attrStrTextField cell]];
Run Code Online (Sandbox Code Playgroud)

  • 您可以传递大小的"0.0"以获得默认的常规大小.您也可以向NSFont询问任何其他尺寸(例如,小型控件和单元格的尺寸).您通常不应传递明确的大小. (2认同)