如何创建CALayer文本发光效果

时间:2013-01-11 14:25:43

标签: iphone ios calayer glow

我正在关注Brad's answer以在我的CALayer文字中应用发光效果。

这是我的代码:

- (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();

}

我在这里得到了一个体面的绿色光芒。但是,除了发光之外,我希望文本也有自己的颜色。为此,我在这里使用颜色变量,以及CGContextSetFillColorWithColor。但似乎没有效果。文字看似白,带有绿色光芒。我希望主要颜色=颜色和发光=绿色的文本。

我该怎么办?

1 个答案:

答案 0 :(得分:2)

我对你的颜色很困惑...我认为在objective-c中声明红绿蓝时你设置的值最多为1.0(就像你用alpha做的那样)所以当你给它们真实时255十六进制值然后除以255.你的颜色应该是白色的,因为所有3个值都远远高于最大值...也许我错了,虽然先试试这两个代码......

用以下代码替换当前的FillColorWithColor代码:

[[UIColor colorWithCGColor:color] set];

(或许这......)

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

如果它们不起作用,那么在将颜色代码更改为此时尝试它们:

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

相关问题