gpt4 book ai didi

objective-c - NS菜单项 : Truncation and setLineBreakMode

转载 作者:行者123 更新时间:2023-12-03 16:55:12 28 4
gpt4 key购买 nike

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

    NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:t action:s keyEquivalent:e];
[newItem setTarget:target];
[newItem setEnabled:YES];

[self addItem:newItem];

我想像这样截断它们的内容(在中间):

Some really really long title ---> Some rea...title

我读过有关使用 setLineBreakMode 方法的内容...但是如何使用?(我认为我做错了什么:-S)

最佳答案

一个可能的解决方案包括使用 NSAttributedString 和 [NSMenuDelegate confinementRectForMenu:onScreen:screen];

confinementRectForMenu 是设置你想要的最大宽度所必需的

只是为了向您展示一个没有委托(delegate)的示例,下面我展示了一个带有非常大字体的菜单项

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];

关于objective-c - NS菜单项 : Truncation and setLineBreakMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859781/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com