2

Je crée un menu de noms de fichiers et de dates de modification. Je les aligne en utilisant une chaîne attribuée avec un arrêt de tabulation pour correspondre au plus long nom de fichier. Cela fonctionne très bien sur macOS 10.8-10.11.NSMenuItem avec attributeTitle ne fonctionne pas dans macOS 10.12.2 Sierra

C'est ce que le menu devrait ressembler - sur macOS 10.11 et 10.12.1:

macOS10.11 menu

Sur Sierra 10.12.2 il ressemble maintenant à ceci:

macOS10.12 menu

Le code est le même sur toutes les plates-formes:

#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]; 
    } 
} 

Des pensées?

+0

Sous quel macOS, exécutez-vous le débogueur avec quelle version de Xcode? –

+0

La date n'a pas de style de tabulation? – Willeke

+0

HI @ElTomato les captures d'écran n'ont pas été faites en utilisant le débogueur - mais normalement les processus en cours d'exécution. En utilisant Xcode 8.2 –

Répondre

2

J'ai trouvé que définir la propriété NSParagraphStyle'sfirstLineHeadIndent ou headIndent à un nombre supérieur à 0 l'obligerait à fonctionner à nouveau.

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

Super! Cela a fonctionné pour moi! – Klaas

+0

Oui - travaille pour moi aussi - merci - excellent! –