我正在以编程方式创建几个菜单项,如下所示:
NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:t action:s keyEquivalent:e];
[newItem setTarget:target];
[newItem setEnabled:YES];
[self addItem:newItem];我想把它们的内容删减如下(中间):
一些非常长的标题-->一些rea...title
我读过关于使用setLineBreakMode方法.但是怎么做?(我觉得我做错了什么:-S)
发布于 2012-03-25 14:22:00
一个可能的解决方案是使用NSAttributedString和NSMenuDelegate限制:屏幕上的NSMenuDelegate:;
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];https://stackoverflow.com/questions/9859781
复制相似问题