【问题标题】:Get default font name in Cocoa for NSMenuItem?在 Cocoa 中为 NSMenuItem 获取默认字体名称?
【发布时间】:2014-05-02 22:09:30
【问题描述】:

我在我的 NSMenuItem 中使用 NSAttributed NSString,但它的字体与默认字体相比发生了变化,我想为 Attributed 字符串使用默认字体。

谁能解释一下,如何找到或获取 NSMenuItems 的默认字体。

现在我正在使用这个:

 NSDictionary *attributes = @{
                                 NSFontAttributeName: [NSFont fontWithName:@"Helvetica" size:14],
                                 NSForegroundColorAttributeName: [NSColor blackColor],
                                 NSParagraphStyleAttributeName:paragraphStyle

                                 };

谢谢

【问题讨论】:

  • NSFont 提供 systemFont - 你试过吗?

标签: objective-c cocoa nsmenu nsmenuitem


【解决方案1】:

要获得NSMenuItem 的默认字体,只需使用方法[NSFont menuBarFontOfSize:0]

NSDictionary *attributes = @{ NSFontAttributeName: [NSFont menuBarFontOfSize:0],
                              NSForegroundColorAttributeName: [NSColor blackColor],
                              NSParagraphStyleAttributeName: paragraphStyle };

这在所有方面都更容易。

【讨论】:

    【解决方案2】:

    我得到了解决方案,我使用了这个

    NSFont *oldFont = [menu font];
    NSLog(@"%@",oldFont.familyName);
    

    并像这样使用

    NSDictionary *attributes = @{
                                     NSFontAttributeName:[NSFont fontWithName:@"Lucida Grande" size:14],
                                     NSForegroundColorAttributeName: [NSColor blackColor],
                                     NSParagraphStyleAttributeName:paragraphStyle
    
                                     };
    

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2017-11-12
      • 1970-01-01
      • 2011-11-09
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      相关资源
      最近更新 更多