2

如何设置 NSStatus 项的标题文本颜色的文本颜色?

这是我用来设置 statusItem 的:

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"set the timeWSeconds to do shell script \"/bin/date '+%a %b %I:%M:%S %p'\""];


    NSAppleEventDescriptor *timeWSeconds = [[script executeAndReturnError:nil]stringValue];
    [statusItem setTitle:timeWSeconds];

好的,我已经尝试过@Vervious 的帖子,这就是我所拥有的,没有任何改变。

-(IBAction)timeWSeconds:(id)sender
{
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"set the timeWSeconds to do shell script \"/bin/date '+%a %b %d %I:%M:%S %p'\""];


    NSAppleEventDescriptor *timeWSeconds = [[script executeAndReturnError:nil]stringValue];
    [statusItem setTitle:timeWSeconds];

    NSDictionary *attributes = [NSDictionary
                                dictionaryWithObjectsAndKeys:
                                [NSColor redColor], NSForegroundColorAttributeName, nil];
    NSAttributedString *colouredTitle = [[[NSAttributedString alloc]
                                         initWithString:[timeWSeconds stringValue]]
attributes:attributes];
    [statusItem setAttributedTitle:colouredTitle];

}
4

1 回答 1

2

将状态项的属性标题设置为您选择的属性字符串。例如

NSDictionary *attributes = [NSDictionary
                            dictionaryWithObjectsAndKeys:
                            [NSColor redColor], NSForegroundColorAttributeName, nil];
NSAttributedString *colouredTitle = [[NSAttributedString alloc] 
             initWithString:[timeWSeconds stringValue]]
             attributes:attributes];
[statusItem setAttributedTitle:colouredTitle];
于 2012-08-03T20:51:37.293 回答