将 NSMenuItem 的标题椭圆化为菜单的宽度
我想以编程方式更改 NSMenuItem 的标题,但将其省略,以便此修改永远不会导致菜单宽度增加。我该怎么做?
I want to programatically change an NSMenuItem's title, but ellipsize it so that this modification never causes the menu to increase in width. how can i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须自己做这件事。 NSString 有一些 AppKit 添加的方法来确定字符串的长度,给定某种字体等。如果你发现字符串太长,从中间删除一个块,再次测量,一直这样做,直到字符串变短足够的。
然而,这可能有点矫枉过正,因为
NSMenuItem
会自动“省略”标题(尽管这可能会导致超宽的菜单)You have to do this yourself. NSString has some methods added by AppKit for determining the length of a string, given a certain font, etc. If you find the string is too long, remove a chunk from the middle, measure again, and keep doing that until the string is short enough.
However, this might be overkill, since
NSMenuItem
will "ellipsize" the title automatically (although this can result in super-wide menus)