2

ファイル名と変更日のメニューを作成します。私は、最も長いファイル名に合わせてタブストップが設定された属性付き文字列を使用してこれらを整列させます。これはMacOS 10.8-10.11で正常に動作します。シエラ10.12.2で、それは次のようになりますNSMenuItem with attributedTitleがmacOSで動作しない10.12.2 Sierra

macOS10.11 menu

macOS10.12 menu

のMacOS 10.11上および10.12.1 -

これは、メニューがどのように見えるかですコードはすべてのプラットフォームで同じです。

#define FILEICONSIZE   16.0 
#define FILEDATELEADINGSPACE 16.0 

... 

- (void)rebuildMenu:(NSMenu *)menu fromFiles:(NSMutableArray <FileRepresentation *> *)files 
{ 
    NSMenuItem *item = [menu itemWithTitle:NSLocalizedString(@"Open iCloud", nil)]; 
    NSMenu *icloudFilesMenu = item.submenu; 
    if (!icloudFilesMenu) 
     return; 

    static NSImage *icon; 
    if (!icon) { 
     icon = [NSImage imageNamed:@"SSDoc"]; 
     icon.size = NSMakeSize(FILEICONSIZE, FILEICONSIZE); 
    } 

    [icloudFilesMenu removeAllItems]; 

    NSDictionary *stdAttributes = @{ NSFontAttributeName: [NSFont menuBarFontOfSize:0] }; 
    NSDictionary *ttAttributes = @{ NSFontAttributeName: [NSFont toolTipsFontOfSize:0] }; 

    // get max width of filename 
    CGFloat maxWidth = 0; 
    for (FileRepresentation *f in files) { 
     NSMutableAttributedString *attribTitle; 

     attribTitle = [[[NSAttributedString alloc] initWithString:f.fileName attributes:stdAttributes] mutableCopy]; 
     [attribTitle addAttribute:NSParagraphStyleAttributeName 
          value:[NSParagraphStyle defaultParagraphStyle] 
          range:NSMakeRange(0, f.fileName.length)]; 
     NSRect rect = [attribTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) 
               options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading]; 
     if (rect.size.width > maxWidth) 
      maxWidth = rect.size.width; 
    } 
    maxWidth += FILEDATELEADINGSPACE; 
    NSMutableParagraphStyle *tabbedStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    tabbedStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment:NSLeftTextAlignment location:maxWidth options:@{}]]; 

    // build file menu 
    for (FileRepresentation *f in files) { 
     NSMutableAttributedString *attribTitle; 
     NSString *fname; 

     fname = [f.fileName stringByAppendingString:@"\t"]; 
     item = [[NSMenuItem alloc] initWithTitle:fname action:@selector(openFile:) keyEquivalent:@""]; 

     attribTitle = [[[NSAttributedString alloc] initWithString:fname attributes:stdAttributes] mutableCopy]; 
     [attribTitle addAttribute:NSParagraphStyleAttributeName 
          value:tabbedStyle 
          range:NSMakeRange(0, fname.length)]; 

     // append file date in tool tip font 
     if (f.modDate) { 
      NSAttributedString *attribfDate; 
      NSString *fdate = [((AppController *)[(NSApplication *)NSApp delegate]).fileDateFormatter stringFromDate:f.modDate]; 
      attribfDate = [[NSAttributedString alloc] initWithString:fdate attributes:ttAttributes]; 
      [attribTitle appendAttributedString:attribfDate]; 
     } 

     item.attributedTitle = attribTitle; 
     item.target = self; 
     item.enabled = YES; 
     item.representedObject = f.url; 
     item.image = icon; 

     [icloudFilesMenu addItem:item]; 
    } 
} 

どのような考えですか?

+0

どのmacOSで、どのXcodeバージョンでデバッガを実行していますか? –

+0

日付にタブストップスタイルがありませんか? – Willeke

+0

HI @ElTomatoは、デバッガを使用してスクリーンショットを作成したのではなく、通常は実行中のプロセスです。 Xcode 8.2を使用しています –

答えて

2

NSParagraphStyle'sfirstLineHeadIndentまたはheadIndentというプロパティを0より大きい値に設定すると、再び機能することがわかります。

tabbedStyle.tabStops = ... 
tabbedStyle.headIndent = DBL_EPSILON; // A tiny number so the indent is not noticeable 
+0

素晴らしい!これは私のために働いた! – Klaas

+0

はい - 私のためにも働きます - ありがとう - 優秀! –

関連する問題