Subclassing a NSTextField

╄→гoц情女王★ 提交于 2020-01-06 05:34:08

问题


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


回答1:


Martin,

I started with the "New File" dialog and chose "Cocoa" on the left and then Objective-C class.

That will generate the following code, without the comments. Then all you need to do is change the NSObject in the"@interface" line of the header to "NSTextView" and you have a working subclass. If you are using XCode 3.0 you can go to Interface Builder and change class of your NSTextField to "MyTextView".

You should also pick up Aaron Hillegass' book "Cocoa Programming for Mac OS X, Third Edition" It has bee updated for Leopard, if you haven't already.

Good Luck.


#import <Cocoa/Cocoa.h>


@interface MyTextView : NSTextView {
 // Outlets & Members go here
}
// Actions & messages go here
@end


#import "MyTextView.h"


@implementation MyTextView

@end




回答2:


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.




回答3:


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.




回答4:


  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.




回答5:


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



来源:https://stackoverflow.com/questions/606319/subclassing-a-nstextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!