views:

856

answers:

5

First off; I'm a COMPLETE newbie at Cocoa, so please bare with me for asking what I am sure is an inane and utter newbish question.

My situation is as follows: I want to expand on the functionality of NSTextField. AMong the things I want to achieve is:

Changing the look and feel of the caret.

Detecting when the text reaches a certain number of characters and then coloring the text after that limit differently. *

To my great frustration after spending quite some time googling I find hundreds of hits that simply state "Sublass NSTextField and use this code.", and to my humiliation I have found myself unable to grok exactly how to do this.

I would be extremely grateful if someone could give me a working example of a subclass that achieves one of the two things I list above, and instructions* on how to implement the code so I can try and figure out how it works by looking at some actual live code.


I am extremely apologetic for my late response!

Apologies to all of you. I have a colic infant at home, and as you (or at least those of you that have children) can imagine this takes up quite a lot of your available time. Thank you all for your responses.

I see that one of my main problems is that I don't have a sufficient understanding of delegates and outlets. I have purchased the book recommended here (and many other places. Some sort of "Bible" I gather) and I'm looking into it as we speak in the few silent hours I have these days. :)

But although I can see it's going to be an indispensable tool for me I still gain the most understanding from studying examples rather than reading the theory* and so I would be extremely grateful if someone would create a project with a proper subclass of the relevant class since I understand that I should probably not be extending the NSTextfield class?

I would instantly mark Mark Thalmans post as the answer as I'm sure it's a proper "for dummies" response, but I'll hold out for a few days since I'd really love a file to peruse. But I am not ungrateful!

Oh, and; Please believe me guys when I say I'm not quite as useless in languages I actually know. It's just that these concepts with the Interface Builder and GUIs connection to the code is very unknown to me. I usually just write the code and keep it at that.


*Yes, my first little training project is indeed a Twitter Utility.

*Like to a child

*Not that reading the theory hasn't got tremendous value for me as well. I wouldn't be where I am without Colin Moock definitive guide to AS3

+2  A: 
Mark Thalman
+5  A: 

setInsertionPointColor: will take care of setting the caret color, and using delegate methods would be the best way to color the text after the number of characters change. In general, a lot of classes in Cocoa are like this; you can subclass them, but most of the functionality you need to change are in delegate methods.

Marc Charbonneau
+4  A: 

NSTextField is special, because it doesn’t actually implement text editing. That’s done by a shared (per-window) NSTextView, known as the field editor. You can provide a special field editor for a given NSTextField. This is canonically done by subclassing NSWindow (!) and overriding -fieldEditor:forObject:. When I was looking this up, though, I found NSTextFieldCell’s -setUpFieldEditorAttributes: method, which looks as though it could return a different field editor than the one it’s handed.

Recommended reading: Control and Cell Programming Topics for Cocoa, Text System Overview.

Ahruman
+3  A: 
  1. If you really have to subclass it, you have to subclass the NSTextFieldCell. Informations about NSCells are available online.

  2. Don't subclass the cell if not absolutely necessary. Use the delegate methods.

  3. At least the color can be changed using NSTextField's bindings, use those.

Georg
+1  A: 

You may also be able to get some of the functionality required with a formatter.

Matthew Schinckel