objective-c - 设置 NSStatus 项的标题文本颜色

标签 objective-c macos cocoa colors nscolor

如何设置 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];

}

最佳答案

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

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

关于objective-c - 设置 NSStatus 项的标题文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801277/

相关文章:

objective-c - Xcode 不断要求输入密码以使用系统钥匙串(keychain)

iphone - 如何解决objective-c中的命名空间冲突?

ios - 如何将 iPhone OSStatus 代码转换为有用的代码?

ios - 用于类消息的 Objective-C 宏

cocoa - 如何使坞站重新加载所有坞站磁贴插件?

objective-c - NSTextView:以编程方式格式化文本

ios - iAd横幅广告在iPhone 6/6 Plus上的位置错误

objective-c - 适用于 iOS 的沙子物理学

macos - Sketch App 的 Apple 脚本

macos - MacOSX 上的智能卡 PKCS#11 支持