NSMenuItem with attributedTitle not working in macOS 10.12.2 Sierra only
| Originator: | michael | ||
| Number: | rdar://29795413 | Date Originated: | 23-Dec-2016 |
| Status: | Open | Resolved: | |
| Product: | macOS + SDK | Product Version: | |
| Classification: | Reproducible: | Yes |
I create a menu of file names and modification dates. I align these using an attributed string with a tab stop set to fit the longest file name. This works fine on macOS 10.8-10.11 and 10.12.1. However on 10.12.2 the tab stop isn't expanded correctly.
Steps to Reproduce:
Set attributedTitle on NSMenuItem, using a tab stop to leave space between the filename and the file modification date. See code:
#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];
}
}
Expected Results:
macOS 10.12.2 should render the attributed string the same was as on macOS 10.12.1, as the attributed strings are the same.
Actual Results:
macOS 10.12.2 appear to ignore the tabStops applied to the style.
Version:
10.12.2 (16C67)
Notes:
Configuration:
Issue occurs on macOS 10.12.2, but not on 10.8, 10.9, 10.10, 10.11, 10.12.1
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!