有关自定义NSComboBox的任何示例? [英] Any example about custom NSComboBox?

查看:1195
本文介绍了有关自定义NSComboBox的任何示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求实现这样的组合框:





很明显,这与不同于NSComboBox

< 1>文本字段旁边的按钮应该是定制的。
< 2>这更加重要:有一个附加的cross(其是指示删除动作的按钮)



如何实现这个效果?我搜索互联网,但没有回答。任何人告诉我应该做什么或告诉我任何子类化或定制NSComboBox类的示例?

解决方案

NSComboBox是一个视图,但是为了原因,我不会进入这里它的接口是由一个伴随的单元格类绘制: NSComboBoxCell 。对于自定义绘图,你通常必须对单元格类型(在这种情况下为NSComboBoxCell)进行子类化,在你的自定义单元格子类中实现你想要的所有绘图,然后使视图使用你的单元格与 -setCellClass: -setCell:方法。



然而,在这种情况下,NSComboBox从NSTextField继承, NSComboBoxCell继承自 NSTextFieldCell ,因此两者都已实现 - setBackgroundColor: - setTextColor:;所以应该很容易绘制白色背景和蓝色文本。



我不知道NSComboBox如何绘制下拉菜单,我看不到任何方式的定制,但我敢肯定,这是可能的。你可能需要在NSCell的绘图方法中玩弄,看看





最后的问题,你会有把十字架内的文本字段。你将必须使用NSCell的绘图方法实现十字架的绘图。然而,为了拦截鼠标点击,你需要使用NSView(因为NSCell不处理交互)。这意味着你还需要子类化NSComboBox并跟踪鼠标何时进入绘制十字架的视图部分。



这很复杂,所以也许有是另一种方式。您是否知道NSPopover?



这将更容易实现类似于NSPopover的东西。 popover持有一个视图,所以你不必乱七八糟的NSCell方法。弹出框可以包含名称列表和一个NSButton(这是十字)。


I am asked to implement an combo box like this:

It is clear that this is quite different from NSComboBox:
<1> The button of beside the text field should be customized
<2> This is much more important: there is an additional "cross" (which is a button indicating "delete" action) in each of the combo item.

How can I achieve this effect? I searched for the internet but fount no answer. Could any one tell me what I should make this or tell me any example of subclassing or customizing NSComboBox class?

解决方案

NSComboBox is a view, but for reasons I won't go into here it's interface is drawn by an accompanying cell class: NSComboBoxCell. For custom drawing you normally have to subclass the cell type (in this case NSComboBoxCell), implement all the drawing you want in your custom cell subclass and then make the view use your cell with the -setCellClass: or -setCell: method.

However, in this case NSComboBox inherits from NSTextField, and NSComboBoxCell inherits from NSTextFieldCell, so both already implement -setBackgroundColor: and -setTextColor:; so it should be fairly easy to draw the white background and blue text.

I'm not sure how NSComboBox draws the 'drop down menu', I can't see any way of customising that, but I'm sure it's possible. You will probably have to play around inside the NSCell's drawing methods to see,

The final problem you will have is putting the cross inside the text field. You will have to implement the drawing of the cross using the NSCell's drawing methods above. However, to intercept the mouse clicks you would need to do that with an NSView (because NSCell's don't deal with interactions). This means you also need to subclass NSComboBox and keep track of when the mouse enters the part of the view where the cross is drawn.

This is all rather complicated so maybe there is another way. Are you aware of NSPopover?

It would be much easier to implement something similar with NSPopover. The popover holds a view so you don't have to mess around with an NSCell methods. The popover could contain the list of names and an NSButton (which is the cross).

这篇关于有关自定义NSComboBox的任何示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆