一個可能的解決方案包括使用NSAttributedString和[NSMenuDelegate confinementRectForMenu:屏幕:屏幕]。
confinementRectForMenu需要設置你想要
只是爲了顯示你沒有委託一個例子,最大寬度低於我展示一個菜單項具有非常大的字體
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle]
mutableCopy];
[style setLineBreakMode:NSLineBreakByTruncatingMiddle];
NSDictionary* paragraphStyle = [[NSDictionary alloc] initWithObjectsAndKeys:
style, NSParagraphStyleAttributeName,
[NSFont fontWithName:@"Lucida Grande" size:43.0f],
NSFontAttributeName,
nil];
[style release];
NSString* title = @"long titlelong titlelong titlelong titlelong titlelong titlelong and another string titlelong titlelong title end";
NSAttributedString* str = [[[NSAttributedString alloc] initWithString:title attributes:paragraphStyle] autorelease];
NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:title action:@selector(showWhitespaces:) keyEquivalent:@""];
[newItem setAttributedTitle:str];
[newItem setEnabled:YES];
[theMenu addItem:newItem];
我碰到了同樣的解決方案來(像往常一樣:發佈問題後),但我有問題設置菜單項的寬度。非常感謝你指出了這一點;我會看看它; ;-) – 2012-03-25 14:27:40