【问题标题】:NSStatusItem too short, text cut offNSStatusItem 太短,文本被截断
【发布时间】:2016-02-03 18:28:43
【问题描述】:

我有一个状态项:

@property (nonatomic, strong) NSStatusItem* item;

我这样创造的:

item=[[NSStatusBar systemStatusBar] statusItemWithLength: NSVariableStatusItemLength];
item.menu= self.menu;
item.image= icon;
item.highlightMode= YES;

图标是20x20,除了图标我还设置了属性标题:

NSDictionary* attr= @{NSForegroundColorAttributeName : [controller color],
                       NSFontAttributeName : [NSFont userFontOfSize: 12.5]};
item.attributedTitle=[[NSAttributedString alloc]initWithString: @"12190" 
                                                attributes: attr];

问题是状态栏还是被截断了:

它应该显示整个数字,但它从数字“9”中截断了。我在还有 OS X 10.8 的时候编写了这个应用程序,并且文本没有被删减。如果我没记错的话,它只适用于 OS X 10.10。

【问题讨论】:

标签: objective-c macos osx-yosemite


【解决方案1】:

在设置 NSStatusItem 标题属性时遇到了类似的问题。当我在 item.title 值中从空文本变为非空文本时,它出现了。第二次更改标题时,文本截断已修复。对我来说,解决方案是在前一个值为空字符串时将item.title 设置两次。

示例代码:

NSString *title = @"Status text";
// If previous value was empty set title twice to fix cut off issue
if (item.title.length == 0)
{
  [item setTitle:title];
}
[item setTitle:title];

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多