简体   繁体   English

带有图像和文本的NSMenuItem

[英]NSMenuItem with Image and text

I have a question regarding NSMenuItems. 我有一个关于NSMenuItems的问题。

What I'm trying to do is replicate a java GUI using native OS X components, therefore the language I am using is Cocoa. 我要做的是使用本机OS X组件复制java GUI,因此我使用的语言是Cocoa。 What I am trying to do is to get every menu item to have an image and then, beside it, some text. 我想要做的是让每个菜单项都有一个图像,然后在它旁边,一些文本。

I have already done some research into it and my first port of call (as always it seems lol) was the apple docs which had this handy example which illustrates how to embed views inside menuitems: 我已经对它进行了一些研究,我的第一个调用端口(一如既往看起来大声笑)是苹果文档,它有一个方便的例子,说明了如何在menuitems中嵌入视图:

https://developer.apple.com/library/mac/#samplecode/MenuItemView/Listings/MyWindowController_m.html#//apple_ref/doc/uid/DTS10004136-MyWindowController_m-DontLinkElementID_8 https://developer.apple.com/library/mac/#samplecode/MenuItemView/Listings/MyWindowController_m.html#//apple_ref/doc/uid/DTS10004136-MyWindowController_m-DontLinkElementID_8

Being relatively new to cocoa, I was thinking I would have to override one of the drawing methods from NSMenuItem. 作为一个相对较新的可可,我以为我必须覆盖NSMenuItem中的一个绘图方法。 Not really sure though. 虽然不太确定。 Another idea that I was toying with was creating a custom view that held a image and some text. 我正在玩弄的另一个想法是创建一个包含图像和一些文本的自定义视图。

Any other ideas/validation or discussion would be most appreciated. 任何其他想法/验证或讨论将是最受欢迎的。

Thanks all! 谢谢大家!

Oh and the GUI creation is being done by hand no interface builder. 哦,GUI创建是手工完成的,没有界面构建器。

Okay, so I now have menu items with icons beside them. 好的,所以我现在有了旁边有图标的菜单项。 For anyone who is interested here it is ( i've not done a leak analysis on it or anything). 对于任何对此感兴趣的人来说(我没有对它做任何泄漏分析)。

First things first, put all of the images you want into the "Resources" folder (thats what its called in xcode 3.1.4). 首先,将所需的所有图像放入“Resources”文件夹(这就是xcode 3.1.4中所称的内容)。

Now, for example, after we have all the images, we want to use images called "eraser.png" and "eraser_on.png" and I want to attach these to the 3rd menu item. 现在,例如,在我们拥有所有图像之后,我们想要使用名为“eraser.png”和“eraser_on.png”的图像,并且我想将它们附加到第3个菜单项。 In order to achieve this we do the following : 为了实现这一目标,我们执行以下操作:

The code below will get the menu item at position 3 in the menu 下面的代码将获得菜单中位置3的菜单项

NSMenuItem *item = [ nameOfPopUpButton itemAtIndex:2];

The code below will set the image for the menu item to be "eraser.png" 下面的代码将菜单项的图像设置为“eraser.png”

[ item setImage: [ NSImage imageNamed:@"eraser"] ];

That's you set the image for the menu item (which will be on the left hand side of the text aka before the text). 那就是你设置菜单项的图像(它将在文本的左侧,即文本之前)。

If you want different images for the different states, eg when the user presses it, use this method (not tested myself but its sounds sensible :D and the function is straight out the api) 如果你想要不同状态的不同图像,例如当用户按下它时,使用这种方法(不是我自己测试但它的声音合理:D和功能直接api)

[item setOnStateImage: [ NSImage imageNamed:@"eraser_on" ] ]

You can however leave it nil or not set it at all and it will go the default color 但是你可以将它设置为零或根本不设置它,它将成为默认颜色

Hope this helps someone. 希望这有助于某人。

Pieced this together from: https://developer.apple.com/library/mac/#samplecode/MenuMadness/Listings/Controller_m.html#//apple_ref/doc/uid/DTS40008870-Controller_m-DontLinkElementID_4 从以下网址拼凑而成: https//developer.apple.com/library/mac/#samplecode/MenuMadness/Listings/Controller_m.html#//apple_ref/doc/uid/DTS40008870-Controller_m-DontLinkElementID_4

Thanks :) 谢谢 :)

If you need to do this you have the right idea in creating a view with image and label subviews. 如果您需要这样做,您在创建包含图像和标签子视图的视图时有正确的想法。

BUT: don't do this. 但是:不要这样做。 Creating a "native" application is not primarily about your choice of language (which is Objective-C, btw, not Cocoa; the latter is a collection of development frameworks implemented in Objective-C). 创建“本机”应用程序主要不是因为您选择的语言(这是Objective-C,顺便说一句,而不是Cocoa;后者是在Objective-C中实现的开发框架的集合)。 It's about conforming to the platform. 这是关于顺应平台。

On OS X (and iOS), more than probably any other platform, consistency in UI design is paramount. 在OS X(和iOS)上,可能比任何其他平台更多,UI设计的一致性至关重要。 Users know when an application looks strange, and having icons next to each menu item (something I certainly have seen in Java apps) is definitely strange and unnatural on OS X. Users will be irritated at best, confused at worst. 用户知道什么时候应用程序看起来很奇怪,并且每个菜单项旁边都有图标(我在Java应用程序中肯定会看到的东西)在OS X上肯定是奇怪和不自然的。用户最多会感到烦躁,最糟糕的是混淆。

So my advice is to either follow the Human Interface Guidelines (and save yourself a lot of work as a nice side effect) or just stick with your existing Java application. 因此,我的建议是遵循人机界面指南(并为自己节省大量工作作为一个很好的副作用)或者只是坚持使用现有的Java应用程序。

If you want to provide quick iconic access to common functions, the recommended approach on OS X is to use a toolbar. 如果要提供对常用功能的快速标志性访问,OS X上推荐的方法是使用工具栏。

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

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