首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMenuItem :截断和setLineBreakMode

NSMenuItem :截断和setLineBreakMode
EN

Stack Overflow用户
提问于 2012-03-25 11:28:53
回答 1查看 1.3K关注 0票数 1

我正在以编程方式创建几个菜单项,如下所示:

代码语言:javascript
复制
    NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:t action:s keyEquivalent:e];
    [newItem setTarget:target];
    [newItem setEnabled:YES];

    [self addItem:newItem];

我想把它们的内容删减如下(中间):

一些非常长的标题-->一些rea...title

我读过关于使用setLineBreakMode方法.但是怎么做?(我觉得我做错了什么:-S)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-25 14:22:00

一个可能的解决方案是使用NSAttributedString和NSMenuDelegate限制:屏幕上的NSMenuDelegate:;

confinementRectForMenu对于设置所需的最大宽度是必要的。

为了给您展示一个没有委托的示例,下面我展示了一个字体非常大的菜单项。

代码语言:javascript
复制
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];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9859781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档