{"id":113,"date":"2006-01-08T11:38:52","date_gmt":"2006-01-08T19:38:52","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/?p=113"},"modified":"2018-10-31T10:15:16","modified_gmt":"2018-10-31T17:15:16","slug":"cocoa-creating-a-custom-color-picker-in-cocoa-part-1","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2006\/01\/cocoa-creating-a-custom-color-picker-in-cocoa-part-1\/","title":{"rendered":"Cocoa: Creating a custom Color Picker in Cocoa. Part 1."},"content":{"rendered":"<p id=\"top\" \/>It is pretty easy to create a custom Color Picker that is available in any application, or for just your particular app. It isn&#8217;t difficult to do, but there are some caveats that you must be aware of.<\/p>\n<p>I&#8217;m going to go over this as a step-by-step tutorial; we need more Cocoa tutorials out there, so this will be one of them!<\/p>\n<p>Start by creating a new Cocoa Bundle inside of Xcode (File | New Project&#8230;)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/NewBundleInXcode.png\" height=\"56\" width=\"190\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Newbundleinxcode\" \/><\/p>\n<p>Now, be careful what you name your bundle &#8212; the name must correspond to the main color picker&#8217;s classname.<\/p>\n<p>Once you have your project created, double click on the project in Xcode and set the output directory to be ~\/Library\/ColorPickers :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/ColorPickersOutputDirectory.png\" height=\"223\" width=\"570\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Colorpickersoutputdirectory\" \/><\/p>\n<p>Note that in order for this to work correctly in Xcode 2.0, you must have Xcode&#8217;s user default UsePerConfigurationBuildLocations set to NO. You can do this from the command line with:<\/p>\n<p>defaults write com.apple.xcode UsePerConfigurationBuildLocations NO<\/p>\n<p>Read the <a href=\"http:\/\/\/Developer\/ADC%20Reference%20Library\/releasenotes\/DeveloperTools\/Xcode\/XcodeDefaults.html\">Xcode Expert Preferences Notes<\/a> for more information on this option.<\/p>\n<p>Next, double click on the bundle target to bring up its options<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/BundleTargetPicture.png\" height=\"37\" width=\"158\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Bundletargetpicture\" \/><\/p>\n<p>In the build options, set the <b>Wrapper Extension<\/b> to be <b>colorPicker<\/b> and the <b>Installation Directory<\/b> to <b>$(HOME)\/Library\/ColorPickers<\/b>. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/BuildOptionscolorPicker.png\" height=\"328\" width=\"393\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Buildoptionscolorpicker\" \/><\/p>\n<p>Then, on the Properties tab, set the Principal Class to be your new Color Picker (which we actually haven&#8217;t made yet):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/ColorPickerPrincipleClass.png\" height=\"250\" width=\"398\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Colorpickerprincipleclass\" \/><\/p>\n<p>Once you have your project setup, you can then start creating your UI in Interface Builder by creating a new empty nib:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/IBCreateNewEmpty.png\" height=\"318\" width=\"349\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Ibcreatenewempty\" \/><\/p>\n<p>Then, you can drag a new view into the nib (note: you must have Tiger to do this!):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/NewViewInNib2.png\" height=\"396\" width=\"523\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Newviewinnib2\" \/><\/p>\n<p>Next, you can setup the UI to look the way you want, save the nib, and add it to the Xcode project (I called it \u00e2\u20ac\u0153MyPicker.nib\u00e2\u20ac\u009d):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/ColorPickerUI.png\" height=\"145\" width=\"209\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Colorpickerui\" \/><\/p>\n<p>Now we want to jump back to XCode and create a controller for the nib file, and the main color picker class.<\/p>\n<p>A long time ago, we decided to call it \u00e2\u20ac\u0153RadiantColorPicker\u00e2\u20ac\u009d. In XCode, select \u00e2\u20ac\u0153File | New&#8230;\u00e2\u20ac\u009d and create an Objective C Class named RadiantColorPicker:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/CreatingTheRadiantClass.png\" height=\"453\" width=\"539\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Creatingtheradiantclass\" \/><\/p>\n<p>Change the class to inherit from NSColorPicker and declare that it is going to implement the NSColorPickingCustom formal protocol. <\/p>\n<p>Some important things to note here: In Tiger and Panther, you MUST descend from NSColorPicker. The documentation incorrectly states that you can implement NSColorPickingDefault or descend from NSColorPicker (note that this will be fixed). <\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 24.0px; text-indent: -24.0px\"><font face=\"Monaco\" size=\"2\" color=\"#760f50\" style=\"font: 10.0px Monaco; color: #760f50\">@interface<\/font><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\"> RadiantColorPicker : NSColorPicker &lt;NSColorPickingCustom&gt; {<\/font><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 24.0px; text-indent: -24.0px\"><font face=\"Monaco\" size=\"2\" color=\"#760f50\" style=\"font: 10.0px Monaco; color: #760f50\">@private<\/font><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 48.0px; text-indent: -48.0px\"><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\"><span class=\"Apple-converted-space\">\u00c2\u00a0 \u00c2\u00a0 <\/span><\/font><font face=\"Monaco\" size=\"2\" color=\"#236e25\" style=\"font: 10.0px Monaco; color: #236e25\">\/\/ Add two outlets to access things in the nib file we will load<\/font><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 48.0px; text-indent: -48.0px\"><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\"><span class=\"Apple-converted-space\">\u00c2\u00a0 \u00c2\u00a0 <\/span><\/font><font face=\"Monaco\" size=\"2\" color=\"#760f50\" style=\"font: 10.0px Monaco; color: #760f50\">IBOutlet<\/font><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\"> NSView *mainView;<\/font><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 48.0px; text-indent: -48.0px\"><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\"><span class=\"Apple-converted-space\">\u00c2\u00a0 \u00c2\u00a0 <\/span><\/font><font face=\"Monaco\" size=\"2\" color=\"#760f50\" style=\"font: 10.0px Monaco; color: #760f50\">IBOutlet<\/font><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\"> NSTextField *textField;<\/font><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 24.0px; text-indent: -24.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 24.0px; text-indent: -24.0px\"><font face=\"Monaco\" size=\"2\" style=\"font: 10.0px Monaco\">}<\/font><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 24.0px; text-indent: -24.0px\"><font face=\"Monaco\" size=\"2\" color=\"#760f50\" style=\"font: 10.0px Monaco; color: #760f50\">@end<\/font><\/p>\n<p>The \u00e2\u20ac\u0153mainView\u00e2\u20ac\u009d will be the view that is placed in the Color Picker, and the textField will give us access to our TextField. To set those things up in the nib file, drag the RadiantColorPicker.h file from Xcode and drop it into the nib file in Interface Builder.<\/p>\n<p>Click on the \u00e2\u20ac\u0153File&#8217;s Owner\u00e2\u20ac\u009d in the Instances of the nib file, and set the custom class to be it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/SettingTheCustomClass-1.png\" height=\"469\" width=\"400\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Settingthecustomclass-1\" \/><\/p>\n<p>Then, hook up the outlets. It is important to drag the mainView outlet to the actual view itself:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/SettingTheMainViewOutlet.png\" height=\"287\" width=\"410\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Settingthemainviewoutlet\" \/><\/p>\n<p>(okay&#8230;this is just a start. I&#8217;ll eventually finish this).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is pretty easy to create a custom Color Picker that is available in any application, or for just your particular app. It isn&#8217;t difficult to do, but there are some caveats that you must&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2006\/01\/cocoa-creating-a-custom-color-picker-in-cocoa-part-1\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[4,6,1],"tags":[63,130,72],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-apple","category-cocoa","category-general","tag-cocoa","tag-objc","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":5327,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/5327"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}