Custom NSTextField Cell

  • Hi,

    This seems like it would be easy, but I can't find anything on it.
    Here's what I want to do: I have a subclass of NSTextFieldCell that
    draws itself a little differently that the standard one. I want to
    embed it in an NSTextField, but Interface Builder doesn't have any
    way to do this. I subclassed the NSTextField itself, too, but of
    course it only gets an initWithCoder: message when it's pulled out of
    the nib, and I have no way of specifying that it uses my cell instead
    of the default NSTextFieldCell.

    So, in short, how do I tell NSTextField to use my cell instead of the
    default one?

    Right now I'm using the code below. It seems like it's a pretty bad
    hack, but it does seem to work. If there's no simple way to
    substitute a control's cell for another, what do you think of this?
    Is there anything wrong or dangerous with it?

    (This code comes from my NSTextField subclass):

    - (id)initWithCoder:(NSCoder *)coder
    {
        [(NSKeyedUnarchiver *)coder setClass:
            [AOEditLabelCell class] forClassName:@"NSTextFieldCell"];
        return [super initWithCoder:coder];
    }

    Thanks,
    Todd
  • How about on awakeFromNib, you tell your text field -
    setCell:myCustomCellInstance ... ?

    On Aug 2, 2005, at 3:44 PM, Todd Yandell wrote:

    > Hi,
    >
    > This seems like it would be easy, but I can't find anything on it.
    > Here's what I want to do: I have a subclass of NSTextFieldCell that
    > draws itself a little differently that the standard one. I want to
    > embed it in an NSTextField, but Interface Builder doesn't have any
    > way to do this. I subclassed the NSTextField itself, too, but of
    > course it only gets an initWithCoder: message when it's pulled out
    > of the nib, and I have no way of specifying that it uses my cell
    > instead of the default NSTextFieldCell.
    >
    > So, in short, how do I tell NSTextField to use my cell instead of
    > the default one?
    >
    > Right now I'm using the code below. It seems like it's a pretty bad
    > hack, but it does seem to work. If there's no simple way to
    > substitute a control's cell for another, what do you think of this?
    > Is there anything wrong or dangerous with it?
    >
    > (This code comes from my NSTextField subclass):
    >
    > - (id)initWithCoder:(NSCoder *)coder
    > {
    > [(NSKeyedUnarchiver *)coder setClass:
    > [AOEditLabelCell class] forClassName:@"NSTextFieldCell"];
    > return [super initWithCoder:coder];
    > }
    >
    > Thanks,
    > Todd
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/dev%
    > 40silentalcove.net
    >
    > This email sent to <dev...>
    >
  • On Aug 2, 2005, at 2:54 PM, SA Dev wrote:

    > How about on awakeFromNib, you tell your text field -
    > setCell:myCustomCellInstance ... ?

    Yes, that works. But the problem is doing that loses all of the
    configuration I set in IB. It all goes back to the default settings
    (control size, font size, etc.). It doesn't make any sense to get it
    all set up in IB, just to redo it in code. I'm looking for a way to
    substitute my class and still keep all of the values I set in IB.

    Thanks!
    Todd
  • Then you will need to also subclass NSTextField and have it
    specify your custom cell, rather than its default. Drag the header
    for your custom text field to IB, then use the Custom Class palette
    to tell your pre-configured text field to use your custom class
    instead of NSTextField.

      Does that work for you?

    On Aug 2, 2005, at 4:06 PM, Todd Yandell wrote:

    >
    > On Aug 2, 2005, at 2:54 PM, SA Dev wrote:
    >
    >
    >> How about on awakeFromNib, you tell your text field -
    >> setCell:myCustomCellInstance ... ?
    >>
    >
    > Yes, that works. But the problem is doing that loses all of the
    > configuration I set in IB. It all goes back to the default settings
    > (control size, font size, etc.). It doesn't make any sense to get
    > it all set up in IB, just to redo it in code. I'm looking for a way
    > to substitute my class and still keep all of the values I set in IB.
    >
    > Thanks!
    > Todd
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/dev%
    > 40silentalcove.net
    >
    > This email sent to <dev...>
    >
  • Oh, and FWIW, I can understand why you'd want to do everything in
    IB (the easy way), but there are many situations where you have to do
    things programmatically. This isn't one of them (though extra steps
    are involved), but you shouldn't be hesitant to do things in code. If
    you can set it in IB, you can set it in code.

    On Aug 2, 2005, at 4:06 PM, Todd Yandell wrote:

    >
    > On Aug 2, 2005, at 2:54 PM, SA Dev wrote:
    >
    >
    >> How about on awakeFromNib, you tell your text field -
    >> setCell:myCustomCellInstance ... ?
    >>
    >
    > Yes, that works. But the problem is doing that loses all of the
    > configuration I set in IB. It all goes back to the default settings
    > (control size, font size, etc.). It doesn't make any sense to get
    > it all set up in IB, just to redo it in code. I'm looking for a way
    > to substitute my class and still keep all of the values I set in IB.
    >
    > Thanks!
    > Todd
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/dev%
    > 40silentalcove.net
    >
    > This email sent to <dev...>
    >
  • > Can't you just drop a standard cell and set it up in
    > interfacebuilder, but then set it's Custom Class (one of the panels
    > in the inspector) to your class? (make sure you drag your class
    > header into your nib window)
    ...
    > Then you will need to also subclass NSTextField and have it specify
    > your custom cell, rather than its default. Drag the header for your
    > custom text field to IB, then use the Custom Class palette to tell
    > your pre-configured text field to use your custom class instead of
    > NSTextField.

    I don't have any problems setting the actual control to use my
    subclassed NSTextField control in IB. The problem lies in the fact
    that Interface Builder, as far as I can tell, will only use
    NSTextFieldCell on the text field, regardless of what class you use
    for the control. So I can have the text field use my subclassed
    NSTextField, but IB still archives it with an NSTextFieldCell as it's
    cell.

    Really, the entire problem is that IB has archived the text field
    using the NSTextFieldCell. When I load the nib my control subclass
    correctly gets the "initWithCoder:" message, just as it should, but
    that coder has NSTextFieldCell archived as it's cell class, *not* my
    cell class. So, I believe that leaves me with two options:

    1. Drop the cell that comes from the archived nib and replace it with
    my own. This is easy enough, but it means that all of the settings I
    set in IB have to be redone in code, so I may as well have created
    the entire control itself in code. This is fine, but I'd rather…

    2. Create the control in IB, set it to use my NSTextField subclass
    *and* my NSTextFieldCell subclass, and not have to worry about
    writing any code to swap out the cell.

    If the second option is simply not possible, that's OK, it's not a
    big deal to write a little code. I just want to make sure I'm not
    missing some setting or panel in IB that let's me use a custom cell
    subclass without having to swap them out in code. Or, am I missing
    something else altogether? What, exactly, did you mean by, "have it
    specify your custom cell"? I tried overriding "+cellClass" in my
    NSTextField subclass, but that had no effect.

    Thanks, I really do appreciate all of your help!
    Todd _______________________________________________
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list      (<Cocoa-dev...>)
    Help/Unsubscribe/Update your Subscription:
    http://lists.apple.com/mailman/options/cocoa-dev/<cocoa...>

    This email sent to <cocoa...>