查找关于接口组件的定制图的信息(Cocoa) [英] Looking for info on custom drawing of interface components (Cocoa)

查看:86
本文介绍了查找关于接口组件的定制图的信息(Cocoa)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎越来越多的OS X应用程序,这些天,正在做各种各样的花哨的绘图东西的自定义控件。像是Twitter的,事情,事件盒,版本等应用程序,只是几个....



所以基本上我正在寻找如何开始做这种类型的任何信息的东西。不确定是否只是通过子类化控件和使用自定义绘图,或者如果它是完全不同的东西。



任何帮助都非常感谢。

解决方案

这完全取决于你想做什么。



例如,在Versions中的显示原始属性按钮是一个NSButton子类,因为基本上我们需要的是标准按钮行为,我们自己看看。一个子类化按钮的方法是简单地在NSButton子类中实现你自己的-drawRect:(NSRect)rect方法,但是我们决定坚持在Cocoa中实现NSButton的方式,这意味着大多数绘制都是通过按钮的单元格来完成的,因此实现如下:



在NSButton子类中:

  +(Class)cellClass 
{
return [OurButtonCell class];
}

- (void)drawRect:(NSRect)rect
{
//首先获取单元格在我们的边界内绘制
//然后如果合适,绘制一个聚焦环
}

在NSButtonCell子类(OurButtonCell) / p>

   - (void)drawInteriorWithFrame:(NSRect)rect inView:(NSView *)controlView 
{
/ /一堆绘图代码
}

Versions中的时间轴视图实际上是一个WebView,您在其中看到的页面使用javascript来折叠您点击的标题。



我使用自定义控件开始的经验法则是:




  • 自定义标准Cocoa控件的外观:


    • 子类化相应的控件(例如NSButton和NSButtonCell)

    • 尽可能接近默认控件的实现方式(例如,在buttoncell中,从现有的属性Title实例方法开始绘制按钮标题,除非您总是想使用相同的属性绘制,而不管什么是在IB中设置或如果您需要根据状态绘制不同的属性,例如在版本主窗口中使用试用到期按钮)


  • 创建一个全新的UI元素:


    • 子类NSView并实现几乎所有的鼠标和键事件处理(在视图中,不需要重做


    • 显示复杂的任意高度,但不是表格:


      • 查看是否可以在HTML,CSS和JS中执行,并将其显示在WebView中。 Web是伟大的布局文本,所以如果你可以卸载这个责任到您的WebView,这可以大大节省痛苦的脖子上。




    有关学习如何在自定义视图的绘制方法中绘制内容的建议阅读: Cocoa Drawing Guide



    定制一个NSTableView的外观是一个完全另一杯茶,由于tableview的复杂性,可以发生在所有的地方。你将实现你自己的自定义单元格为一些你想要在一个表中做的事情,但必须改变行在实际的NSTableView对象本身的子类中突出显示的方式。例如,在 Matt Gemmell的网站上查看iTableView的源代码,以清楚说明在哪里绘制内容。



    最后,我认为Abizer的建议去检查BWToolkit的代码是一个好主意。它可能有点压倒性的,但如果你可以阅读和理解代码,你将没有任何麻烦实施自己的自定义视图和控件。


    It seems like more and more OS X apps these days are doing all kinds of fancy drawing stuff for custom controls. Apps like Twitterific, Things, EventBox, Versions just to name a few....

    So basically I'm looking for any information on how to get started doing this kind of thing. Not sure if it is just done by subclassing controls and using custom drawing or if it is something entirely different.

    Any help is greatly appreciated. THanks!

    解决方案

    It depends entirely on what you want to do.

    The "Show Raw Properties" button in Versions for instance is an NSButton subclass, because basically what we needed is standard button behavior with our own look.  One way to subclass a button is to simply implement your own -drawRect:(NSRect)rect method in the NSButton subclass, but we decided to stick with the way NSButton is implemented in Cocoa, meaning most drawing is done by the button's cell, so the implementation looks like this:

    In the NSButton subclass:

    + (Class) cellClass
    {
        return [OurButtonCell class];
    }
    
    - (void)drawRect:(NSRect)rect 
    {
        // first get the cell to draw inside our bounds
        // then draw a focus ring if that's appropriate
    }
    

    In the NSButtonCell subclass (OurButtonCell):

    - (void)drawInteriorWithFrame: (NSRect) rect inView: (NSView *) controlView
    {
        // a bunch of drawing code
    }
    

    The Timeline view in Versions is actually a WebView, the page that you see in it uses javascript to collapse headers you click on.

    The rule of thumb I use for where to start out with a custom control is:

    • To customize the look of a standard Cocoa control:
      • subclass the appropriate control (like e.g. NSButton and NSButtonCell)
      • stick as close as makes sense to the way the default control is implemented (e.g. in a buttoncell, start from the existing attributedTitle instance method to draw the button title, unless you always want to draw with the same attributes regardless of what's set up in IB or if you need to draw with different attributes based on state, such as with the trial expiration button in Versions' main window)
    • Creating an entirely new UI element:
      • subclass NSView and implement pretty much all mouse and key event handling (within the view, no need to redo "hitTest:") and drawing code yourself.
    • To present something that's complex, of arbitrary height, but isn't a table:
      • See if you can do it in HTML, CSS and JS and present it in a WebView.  The web is great at laying out text, so if you can offload that responsibility to your WebView, that can be a huge savings in pain in the neck.

    Recommended reading on learning how to draw stuff in your own custom view's drawing methods: Cocoa Drawing Guide

    Customizing the look of for instance an NSTableView is an entirely other cup of tea, thanks to the complexity of a tableview, that can happen all over the place.  You'll be implementing your own custom cells for some things you want to do in a table, but will have to change the way rows are highlighted in a subclass of the actual NSTableView object itself.  See for instance the source code for iTableView on Matt Gemmell's site for a clear example of where to draw what.

    Finally, I think Abizer's suggestion to go check out the code of BWToolkit is a great idea.  It might be a bit overwhelming at first, but if you can read and understand that code you'll have no trouble implementing your own custom views and controls.

    这篇关于查找关于接口组件的定制图的信息(Cocoa)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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