Domanda

I have a NSStatusItem in my app that can be configured to show some text. 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. 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.

So, is there another way to precalculate how long a NSStatusItem needs to be to hold some text and optionally an icon? 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?

È stato utile?

Soluzione

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. Add padding to taste.

Another way, using private APIs, is to set the longest string, and then grab the width as follows:

[[statusItem _window] frame].size.width
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top