Seb
Seb

Reputation: 3564

iOS set text color in a view's drawrect

I have code that does custom rendering inside of a drawRect method in a UIView subclass. I am trying to render text out using [NSString drawInRect] which works great, however it always shows up in white. After much googling and browsing SO I have no been able to find out how to change the color of a text. Any advice would be great.

Edit Below is the snippet of code inside my drawRect implementation

if(self.state != UIControlStateHighlighted)
{
    CGContextSaveGState(context);
    CGContextSetFillColorWithColor(context, color);
    CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3.0, shadowColor);
    CGContextAddPath(context, path);
    CGContextFillPath(context);
    [title drawInRect:rect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    CGContextRestoreGState(context);
}

Upvotes: 2

Views: 6675

Answers (2)

amfool
amfool

Reputation: 71

Before drawing text, you have to set the color. Try this,

[[UIColor blackColor] set]; 
[title drawInRect:rect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]]; 

Upvotes: 7

Lily Ballard
Lily Ballard

Reputation: 185681

Have you tried changing the current fill color?

[[UIColor redColor] setFill];
[string drawInRect:rect withFont:font];

Upvotes: 3

Related Questions