简体   繁体   English

获取NSStatusItem的长度

[英]Get length of NSStatusItem

I have a NSStatusItem in my app that can be configured to show some text. 我的应用程序中有一个NSStatusItem,可以将其配置为显示一些文本。 This text can change, and the problem is, every time it changes length, the whole status item changes its length, which causes "width flicker". 该文本可以更改,问题是,每更改一次长度,整个状态项就会更改其长度,这会导致“宽度闪烁”。

I thought about setting the length of the status item to NSVariableStatusItemLength , inserting the longest possible text, get the length back, and then set that as a constant length for the status item. 我考虑过将状态项的长度设置为NSVariableStatusItemLength ,插入尽可能长的文本,取回长度,然后将其设置为状态项的恒定长度。 However, it looks like the length function of NSStatusItem is entirely useless for this, as it simply returns back NSVariableStatusItemLength instead of the actual length. 但是,看起来NSStatusItemlength函数NSStatusItem完全没有用,因为它只是返回NSVariableStatusItemLength而不是实际长度。

So, is there another way to precalculate how long a NSStatusItem needs to be to hold some text and optionally an icon? 因此,还有另一种方法可以预先计算NSStatusItem容纳文本和图标的时间。 For instance, is the icon & font face / size guaranteed to be the same on all Mac systems, so I could simply "measure" an optimal length on my system? 例如,是否在所有Mac系统上都确保图标和字体/大小相同,因此我可以简单地“测量”系统上的最佳长度?

A workaround to get the length is to use [NSImage size].width to get the width of any icons in the status item, and then use [NSString sizeWithAttributes:nil].width to get the width of the string. 一种获取长度的解决方法是使用[NSImage size].width获取状态项中任何图标的宽度,然后使用[NSString sizeWithAttributes:nil].width获取字符串的宽度。 Add padding to taste. 加入调味料。

Another way, using private APIs, is to set the longest string, and then grab the width as follows: 使用私有API的另一种方法是设置最长的字符串,然后按以下方式获取宽度:

[[statusItem _window] frame].size.width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM