NSTextField setTextColor: not working as expected

  • this works:

    [myTextField setTextColor:[NSColor redColor]];

    this doesn't work:

    [myTextField setTextColor:[[NSColor redColor] colorWithAlphaComponent:0.5]];
    (result is plain red, ignoring alpha)

    this doesn't work:

    [myTextField setTextColor:[NSColor colorWithCalibratedRed:0.123
    green:0.456 blue:0.789 alpha:1.0]];
    (result is black, regardless of what the individual components are set to)

    what am i missing here?

    thanks.

    - chuck
  • >
    > this doesn't work:
    >
    > [myTextField setTextColor:[[NSColor redColor]
    > colorWithAlphaComponent:0.5]];
    > (result is plain red, ignoring alpha)
    >
    Documentation  of 'colorWithAlphaComponent' says:
    "A subclass with explicit opacity components should override this
    method to return a color with the specified alpha."
    Thus the above code wont return you the expected color.

    Regards
    Shripada
  • On Dec 31, 2007, at 7:55 AM, Shripada Hebbar wrote:

    >>
    >> this doesn't work:
    >>
    >> [myTextField setTextColor:[[NSColor redColor]
    >> colorWithAlphaComponent:0.5]];
    >> (result is plain red, ignoring alpha)
    >>
    > Documentation  of 'colorWithAlphaComponent' says:
    > "A subclass with explicit opacity components should override this
    > method to return a color with the specified alpha."
    > Thus the above code wont return you the expected color.

    Actually, colorWithAlphaComponent: will indeed return the expected
    color.  I've use that API on many occasions and just created a test
    app with two NSTextField instances.  They use alpha of 0.5 and 0.25
    respectively.  The text was rendered correctly even when the text was
    being edited (i.e. the field editor also did the right thing).

    I ran my test on 10.5.1

    I believe the problem may be within the cells/views hierarchy.  If
    things are marked as being opaque, alpha can be ignored.  Is a custom
    subclass of NSTextField being used?

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • I don't think that's what that's referring to.

    I have used colorWithAlphaComponent: in this same manner many, many
    times over the years... just not to set the text color of an
    NSTextField.

    In fact in another class in this same app, I'm doing...

        [[[NSColor blackColor] colorWithAlphaComponent:0.60] set];
        [NSBezierPath strokeRect:rect];

    ... which works exactly as expected.

    The problem is only when trying to use the setTextColor: method of an
    NSTextField.

    - Chuck

    On December 31, 2007, Shripada Hebbar wrote:

    >>
    >> this doesn't work:
    >>
    >> [myTextField setTextColor:[[NSColor redColor]
    >> colorWithAlphaComponent:0.5]];
    >> (result is plain red, ignoring alpha)
    >>
    > Documentation  of 'colorWithAlphaComponent' says:
    > "A subclass with explicit opacity components should override this
    > method to return a color with the specified alpha."
    > Thus the above code wont return you the expected color.
    >
    >
    > Regards
    > Shripada
  • On Dec 30, 2007, at 11:50 PM, charlie wrote:

    > this works:
    >
    > [myTextField setTextColor:[NSColor redColor]];
    >
    > this doesn't work:
    >
    > [myTextField setTextColor:[[NSColor redColor]
    > colorWithAlphaComponent:0.5]];
    > (result is plain red, ignoring alpha)
    >
    > this doesn't work:
    >
    > [myTextField setTextColor:[NSColor colorWithCalibratedRed:0.123
    > green:0.456 blue:0.789 alpha:1.0]];
    > (result is black, regardless of what the individual components are
    > set to)

    All these work for me (10.5.1)

    > what am i missing here?

    You're going to have to post more info (e.g. what OS you're using).
    Also, what type is myTextField? Custom subclass?  What container
    object(s) is the text field placed into?

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • That's a great thought, but actually it's just a plain-old, IB-created
    NSTextField.

    - Chuck

    On December 31, 2007, Ricky Sharp wrote:

    >
    > On Dec 31, 2007, at 7:55 AM, Shripada Hebbar wrote:
    >
    >>>
    >>> this doesn't work:
    >>>
    >>> [myTextField setTextColor:[[NSColor redColor]
    >>> colorWithAlphaComponent:0.5]];
    >>> (result is plain red, ignoring alpha)
    >>>
    >> Documentation  of 'colorWithAlphaComponent' says:
    >> "A subclass with explicit opacity components should override this
    >> method to return a color with the specified alpha."
    >> Thus the above code wont return you the expected color.
    >
    >
    > Actually, colorWithAlphaComponent: will indeed return the expected
    > color.  I've use that API on many occasions and just created a test
    > app with two NSTextField instances.  They use alpha of 0.5 and 0.25
    > respectively.  The text was rendered correctly even when the text was
    > being edited (i.e. the field editor also did the right thing).
    >
    > I ran my test on 10.5.1
    >
    > I believe the problem may be within the cells/views hierarchy.  If
    > things are marked as being opaque, alpha can be ignored.  Is a custom
    > subclass of NSTextField being used?
    >
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today