NSTextField does not change text color

Discussion in 'Mac Programming' started by kamy, Dec 14, 2014.

  1. kamy macrumors member

    Joined:
    Jul 27, 2011
    #1
    Hi Guys,

    I'm having trouble setting Colors to an 'NSTextField' instance.

    Here are some screenshots

    I set the RGB values using 'colorWithCalibratedRed' API of NSColor to the 'NSTextField' instance. But the values dont seem to change

    Green label here
    Screen Shot 2014-12-15 at 10.44.10 am.png

    Changing the Red value makes a change to yellow
    Screen Shot 2014-12-15 at 10.44.19 am.png

    But changing the Red value does not change the output - it is yellow always
    Screen Shot 2014-12-15 at 10.44.36 am.png

    Adding a value to the Blue textbox, removes the color!
    Screen Shot 2014-12-15 at 10.44.44 am.png

    This is the code - where 'self.color' is an instance of NSTextField
    NSTextField *color;

    Code:
    - (IBAction)onAddCharacter:(id)sender
    {
        float RedValue = [self.red.stringValue floatValue];
        float GreenValue = [self.green.stringValue floatValue];
        float BlueVal = [self.blue.stringValue floatValue];
        float alphaVal = [self.alpha.stringValue floatValue];
        
        NSColor *myTodayColor = [NSColor colorWithCalibratedRed:RedValue green:GreenValue blue:BlueVal alpha:alphaVal];
        
        [self.color setFont:[NSFont fontWithName:@"Arial" size:48]];
        self.color.refusesFirstResponder = YES;
        [self.color setTextColor:myTodayColor];
        [self.color setStringValue:@"Some Text"];
        //[textField setBackgroundColor:[NSColor blackColor]];
        [self.color setDrawsBackground:YES];
        [self.color setBordered:NO];
        
    }
    
    What can be the problem and what am i missing?

    Thanks in advance!
     
  2. grandM, Dec 14, 2014
    Last edited: Dec 14, 2014

    grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #2
    the class method colorWithCalibratedRed asks for a CGfloat and you're feeding it a float
    maybe that's the problem
     
  3. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #3
    Hey,

    Thanks for responding.

    Do you mean hooking the NXTextField from Interface Builder to the property?

    Yes, that is done.

    Every textbox in IB hooked to a property.

    Code:
    @property (weak) IBOutlet NSTextField *color;
    @property (weak) IBOutlet NSTextField *red;
    @property (weak) IBOutlet NSTextField *blue;
    @property (weak) IBOutlet NSTextField *green;
    @property (weak) IBOutlet NSTextField *alpha;
    
    That is not the problem, even if i hardcode the color - i face the same issues
    Code:
    myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];
    
    self.color.textColor = myTodayColor
    
    Have attached the executable..
    View attachment FirstCocoa.app.zip
     
  4. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    I 've edited my post
    the class method colorWithCalibratedRed asks for a CGfloat and you're feeding it a float
    maybe that's the problem
     
  5. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #5
    I just tried using CGFloat but same issues.

    Even if i hardcode it like this there is an issue....

    Code:
    myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];
    self.color.textColor = myTodayColor
    
     
  6. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #6
    I assume onAddCharacter is hooked up to the button
     
  7. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #7
    Correct
     
  8. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #8
    try 30.0f 255.0f 5.0f 1.0f
    then 255.0f 255.0f 5.0f 1.0f

    ----------

    I think I have it
    you can only feed it decimals smaller than or equal to 1 :) so 0.05 etc
    meaning you must divide the value of RedValue etc
     
  9. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #9
    i just tried what u suggested by hardcoding values, just to try

    This gives me Yellow
    Code:
        NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:0.0f alpha:1.0f];
    
        [self.color setTextColor:myTodayColor];
    
    
    Yellow
    1.png

    The moment i set the BLUE value to 48.0f or even 1.0f - it removes the color!

    Code:
     NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:48.0f alpha:1.0f];
    
    
    [self.color setTextColor:myTodayColor];
    
    2.png

    This definitely is a NSTextField bug, u think?
     
  10. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #10
    I think I have it
    you can only feed it decimals smaller than or equal to 1 so 0.05 etc
    meaning you must divide the value of RedValue etc

    ----------

    try 0.3f 0.5f 0.7f 1.0f
    then 0.9f 0.5f 0.7f 1.0f

    ----------

    by the way when naming variables it is easier to maintain a certain logic
    for instance redValue, blueValue, greenValue, alphaValue
     
  11. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #11
     
  12. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #12
    Oh god!

    The documentation says this

    Code:
    + (NSColor *)colorWithCalibratedRed:(CGFloat)red
                                  green:(CGFloat)green
                                   blue:(CGFloat)blue
                                  alpha:(CGFloat)alpha
    
    Parameters
    red
    The red component of the color object.
    green
    The green component of the color object.
    blue
    The blue component of the color object.
    alpha
    The opacity value of the color object.

    Discussion
    Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.

    So it has to be between 0 and 1! How dumb of me to miss that point!

    So how do we translate R, G, B values into decimal values between 0 to 1?
     
  13. kamy thread starter macrumors member

    Joined:
    Jul 27, 2011
    #13
    Okay!!!
    What i needed to do was to convert the 8-bit int RGB values into a 0.0-1.0 float values. To do this simply divided my int value by 255.0. That gave me the correct float value!

    It works fine now!

    Thanks 'grandM' for your help.

    Moral of the story - read each and every line of the Documentation properly!!!
     
  14. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #14
    Glad to help out
     
  15. ahall52 macrumors newbie

    Joined:
    Feb 3, 2013
    #15
    I quick hack if you are coming from a RGB environment is to simply put
    Code:
    x/255
    where x is the R, G or B value. In the OP's second figure, putting the red value as 30/255 would have achieved the desired effect.
     

Share This Page