Objective c NSMenuItem:截断和setLineBreakMode
我以编程方式创建了几个菜单项,如下所示:Objective c NSMenuItem:截断和setLineBreakMode,objective-c,cocoa,truncate,nsmenuitem,Objective C,Cocoa,Truncate,Nsmenuitem,我以编程方式创建了几个菜单项,如下所示: NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:t action:s keyEquivalent:e]; [newItem setTarget:target]; [newItem setEnabled:YES]; [self addItem:newItem]; 我想这样截断它们的内容(中间): 一些非常长的标题-->一些真正的…标题 我已经阅读了有关使用s
NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:t action:s keyEquivalent:e];
[newItem setTarget:target];
[newItem setEnabled:YES];
[self addItem:newItem];
我想这样截断它们的内容(中间):
一些非常长的标题-->一些真正的…标题
我已经阅读了有关使用
setLineBreakMode
方法的内容。。。但是怎么做?(我想我做错了:-S)一个可能的解决方案是使用NSAttributed字符串和[NSMenuDelegate RestrictionRectformenu:onScreen:screen]
要设置所需的最大宽度,必须使用“限制矩形菜单”
为了在下面展示一个没有委托的示例,我展示了一个大字体的菜单项
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];
一种可能的解决方案包括使用NSAttributedString和[NSMenuDelegate ConstrictmentRectformMenu:onScreen:screen] 要设置所需的最大宽度,必须使用“限制矩形菜单” 为了在下面展示一个没有委托的示例,我展示了一个大字体的菜单项
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];
我遇到了完全相同的解决方案(和往常一样:在发布问题之后),但我在设置菜单项的宽度时遇到了问题。非常感谢你指出这一点;我会调查一下的…;-)我遇到了完全相同的解决方案(和往常一样:在发布问题之后),但我在设置菜单项的宽度时遇到了问题。非常感谢你指出这一点;我会调查一下的…;-)