Setting text color in a NSFormCell

  • Newb question: Is there a way to set/change the color of the text
    displayed in a NSFormCell? NSFormCell does'nt seem to have methods
    like setTextColor or setBackgroundColor etc. so I'm not sure how to
    approach this. At times I need to display red text in certain cells
    when data from a array of sensors goes out of range and set it back
    to black when those elements indicate normal conditions.

    Russ
  • On Oct 23, 2007, at 11:03 AM, R.L. Grigg wrote:

    > Newb question: Is there a way to set/change the color of the text
    > displayed in a NSFormCell?

    Yes.

    > NSFormCell does'nt seem to have methods like setTextColor or
    > setBackgroundColor etc. so I'm not sure how to approach this. At
    > times I need to display red text in certain cells when data from a
    > array of sensors goes out of range and set it back to black when
    > those elements indicate normal conditions.

    NSFormCell does have a -setAttributedStringValue: method. You may,
    however, be able to automate what you're trying to do by using
    NSNumberFormatter.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • On Oct 22, 2007, at 3:37 PM, Nick Zitzmann wrote:
    >
    > On Oct 23, 2007, at 11:03 AM, R.L. Grigg wrote:
    >
    >> Newb question: Is there a way to set/change the color of the text
    >> displayed in a NSFormCell?
    >
    > Yes.
    >
    >> NSFormCell does'nt seem to have methods like setTextColor or
    >> setBackgroundColor etc. so I'm not sure how to approach this. At
    >> times I need to display red text in certain cells when data from a
    >> array of sensors goes out of range and set it back to black when
    >> those elements indicate normal conditions.
    >
    > NSFormCell does have a -setAttributedStringValue: method. You may,
    > however, be able to automate what you're trying to do by using
    > NSNumberFormatter.

    Hi Nick,
    I was able to get setAttributedStringValue working, using an
    attributes dictionary. But I'm having difficulty getting the string
    centered in the cell:

        NSDictionary *textDict=[NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSFont boldSystemFontOfSize:
    10],NSFontAttributeName,
                                    [NSColor
    redColor],NSForegroundColorAttributeName,nil];
        NSAttributedString *attrStr=[[[NSAttributedString alloc]
                                            initWithString:descrip
                                                attributes:textDict]
    autorelease];
        [fcell setAlignment:NSCenterTextAlignment];
        [fcell setAttributedStringValue:attrStr];

    This displays the text (from NSString var 'descrip') in bold red, but
    its left justified. Maybe I need to set NSCenterTextAlignment somehow
    in textDict also, but I don't see how to do that...?
    Thanks,
    Russ
  • On Oct 22, 2007, at 6:35 PM, R.L. Grigg wrote:

    > Hi Nick,
    > I was able to get setAttributedStringValue working, using an
    > attributes dictionary. But I'm having difficulty getting the string
    > centered in the cell:
    >
    > NSDictionary *textDict=[NSDictionary dictionaryWithObjectsAndKeys:
    > [NSFont boldSystemFontOfSize:
    > 10],NSFontAttributeName,
    > [NSColor
    > redColor],NSForegroundColorAttributeName,nil];
    > NSAttributedString *attrStr=[[[NSAttributedString alloc]
    > initWithString:descrip
    > attributes:textDict]
    > autorelease];
    > [fcell setAlignment:NSCenterTextAlignment];
    > [fcell setAttributedStringValue:attrStr];
    >
    > This displays the text (from NSString var 'descrip') in bold red,
    > but its left justified. Maybe I need to set NSCenterTextAlignment
    > somehow in textDict also, but I don't see how to do that...?

    Try using a paragraph style, as in:

        NSMutableParagraphStyle    *paragraphStyle =
    [[[NSMutableParagraphStyle alloc] init] autorelease];
        [paragraphStyle setAlignment: NSCenterTextAlignment];

        NSDictionary *textDict=[NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSFont boldSystemFontOfSize:
    10],NSFontAttributeName,
                                    [NSColor
    redColor],NSForegroundColorAttributeName,
                                    paragraphStyle,
    NSParagraphStyleAttributeName,
                                    nil];
        NSAttributedString *attrStr=[[[NSAttributedString alloc]
                                            initWithString:descrip
                                                attributes:textDict]
    autorelease];
        [fcell setAttributedStringValue:attrStr];

    - Mike
  • On Oct 23, 2007, at 6:09 AM, Michael Babin wrote:
    >
    > Try using a paragraph style, as in:
    >
    > NSMutableParagraphStyle *paragraphStyle =
    > [[[NSMutableParagraphStyle alloc] init] autorelease];
    > [paragraphStyle setAlignment: NSCenterTextAlignment];
    >
    > NSDictionary *textDict=[NSDictionary dictionaryWithObjectsAndKeys:
    > [NSFont boldSystemFontOfSize:
    > 10],NSFontAttributeName,
    > [NSColor
    > redColor],NSForegroundColorAttributeName,
    > paragraphStyle,
    > NSParagraphStyleAttributeName,
    > nil];
    > NSAttributedString *attrStr=[[[NSAttributedString alloc]
    > initWithString:descrip
    > attributes:textDict]
    > autorelease];
    > [fcell setAttributedStringValue:attrStr];

    Thanks, Mike!

    Russ
previous month october 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