objective-c - 带有 attributeTitle 的 NSMenuItem 在 macOS 10.12.2 Sierra 中不起作用

标签 objective-c cocoa nsattributedstring macos-sierra nsmenuitem

我创建了一个文件名和修改日期的菜单。我使用带有制表位设置的属性字符串来对齐它们,以适应最长的文件名。这在 macOS 10.8-10.11 上运行良好。

菜单应如下所示 - 在 macOS 10.11 和 10.12.1 上:

macOS10.11 menu

在 Sierra 10.12.2 上,它现在看起来像这样:

macOS10.12 menu

所有平台上的代码都是相同的:

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

有什么想法吗?

最佳答案

我发现将 NSParagraphStyle 的 firstLineHeadIndentheadIndent 属性设置为大于 0 的数字会使其再次工作。

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

关于objective-c - 带有 attributeTitle 的 NSMenuItem 在 macOS 10.12.2 Sierra 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41231554/

相关文章:

Cocoa:NIB 文件创建的实例的 var 名称是什么?

ios - 使用 +[UIFont preferredFontForTextStyle :xxx]? 时如何有选择地将文本设为斜体

ios - 单个 UILabel 中的粗体和非粗体文本?

objective-c - 如何在 NSToolbar 中获取 NSComboBox 以将消息发送到数据源并委托(delegate)

ios - 在 EKCalendar 中创建可重复的事件

cocoa - 如何将 cocoa 绑定(bind)编写为代码而不是在 Interface Builder 中?

objective-c - 在 Cocoa 中使用可访问性 API 插入格式化文本?

iphone - 如何在 UITableView 中为节标题的高度变化设置动画?

ios - 如何在 iOS 中调整各种 iPhone 屏幕尺寸的字体大小?

cocoa - 在 Cocoa/Carbon 下调试 NULL CGContext