Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 863061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:14:51+00:00 2026-05-15T09:14:51+00:00

Given all the complex things I seem to cover every day, this appears to

  • 0

Given all the complex things I seem to cover every day, this appears to be a “what the heck am I doing wrong that seems to simple?” scenario!

I would like to subclass an NSTextField to change the background color and text color. For simplicity sake (and to help anyone who hasn’t ever subclassed anything before), here is the example of my (simplified) subclass MyNSTextFieldSubclass…

Step 1:

Create the subclass file:

First the header file

@interface MyTextFieldSubclass : NSTextField {
}

@end

And the method file

@implementation MyTextFieldSubclass

-(NSColor *)backgroundColor {
    return [NSColor redColor];
}

-(NSColor *)textColor {
    return [NSColor yellowColor];
}

@end

Step 2:

Drag an NSTextField to a window in Interface Builder, select the Identity tab in the inspector and select the class MyTextFieldSubclass

Step 3:

Save the IB file, build and run the application

Problem

When I run the build, the text field does not reflect the color subclassing. However, I know the subclass is being called because if I add the following method, it gets called on text changes.

-(void)textDidChange:(NSNotification *)notification {
    NSLog(@"My text changed");
}

So why does the color change not occur on the text fields?

I know that I can set the color in IB, but for anyone who has dealt with a lot of UI elements that all need the same styling, subclassing makes life way, way easier.

Ironically, I have never had to subclass an NSTextField before and this one has me stumped.

As usual, any and all help very much appreciated. I’m sure it will turn out to be a “Doh!” moment – just cant see the wood for the trees right now (plus I’m exhausted from watching too much World Cup Football early in the morning which never helps).

=== SOLUTION ===

As offered by Jaanus the solution is to put it into the viewWillDraw method. Thus my (simplified) method would now look like this:

@implementation MyTextFieldSubclass

-(void)viewWillDraw {
    [super setBackgroundColor:[NSColor redColor]];
    [super setTextColor:[NSColor yellowColor]];
}

@end

Thanks guys for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:14:52+00:00Added an answer on May 15, 2026 at 9:14 am

    Preston is correct: to change the colors, you should not subclass, just change textfield properties. (Oh it’s NSTextfield, it probably does not have properties then… well, just use the getter and setter methods or configure it correctly in IB.)

    As to

    So why does the color change not occur on the text fields?

    Because you are confusing getters and setters. In your subclass, you have implemented them as getters, where they just return a color. In reality, they should be setter functions (and this is how they are implemented in the guts of NSTextfield): you pass them a color, and they then go and fiddle with whatever internals NSTextfield has, to make the color change happen.

    EDIT: ok, if you are subclassing because you always want to set a specific color, you would do something like

    -(void)viewDidAppear { // or whatever is the Appkit equivalent
        [super setBackgroundColor:...];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that: The asset pipeline is fairly complex (coming from ASP.NET MVC, this is
Given an HTML page I would like to get all the 'x' files that
I followed all the steps given in this blog. When i am trying to
When displaying all articles of a specific month (given through the urls like this
I can't seem to get this one part right. I was given a input
I might be wrong in my assumption, but it seems to me that many
It is easy to find all stored procs that depends on a given table
Given all the holy wars surrounding various code formatting styles, and many companies' strict
You are given all the prime factors of a number, along with their multiplicities
Having been given the all clear to move code from my Page_Loaded method to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.