1

I am following Brad's answer to apply glow effect in my CALayer text.

Here is my code:

- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)context
{   
    UIGraphicsPushContext(context);
    UIFont *font = [UIFont fontWithName:@"Verdana" size:11.0f];

    CGRect rect = CGRectMake(theLayer.bounds.origin.x + 5, theLayer.bounds.origin.y + 5, theLayer.bounds.size.width - 10, theLayer.bounds.size.height - 10);

    NSString * textToWrite = @"Some text";         

    UIColor *color = [ UIColor colorWithRed: (100.0f)  green: (50.0)  blue:(200.0f) alpha: 1.0f ];

    CGContextSetFillColorWithColor(context, color.CGColor); //this has no effect!!!

    CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), 2.0f, [UIColor greenColor].CGColor);

    [textToWrite drawInRect:rect withFont:font
          lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];       
 
    UIGraphicsPopContext();

}

I am getting a decent green glow here. However I want the text to have its own color too, apart from glow. For this I am using color variable here, along with CGContextSetFillColorWithColor. But it seems to have NO effect. The text seems white, with green glow. I want text with main color = color and glow=green.

What should I do?

Community
  • 1
  • 1
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89

1 Answers1

2

I'm confused by your color... I think when declaring red green and blue in objective-c you set values that are up to 1.0 being the maximum (like you did with alpha) so when you give them their true 255 hex value you then divide by 255. Your color should be white since all 3 values are so far above the maximum... Maybe I'm wrong though first try these two codes...

Replace your current FillColorWithColor code with this:

[[UIColor colorWithCGColor:color] set];

(or maybe this...)

[[UIColor colorWithCGColor:color.CGColor] set];

If they don't work then try them while also changing your color code to this:

UIColor *color = [ UIColor colorWithRed: (100.0f/255.0f) green: (50.0f/255.0f) blue:(200.0f/255.0f) alpha: 1.0f ];

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • 2
    Albert is correct about the color. You need to put it in as less than 1.0. Also your green needs to have an f after it. If you calculate the value your self, it needs to be in the format 0.###f. – Douglas Jan 11 '13 at 15:49
  • Thankyou for the confirmation :) – Albert Renshaw Jan 11 '13 at 15:50