【问题标题】:How to create CALayer text glow effect如何创建CALayer文字发光效果
【发布时间】:2013-01-11 14:25:42
【问题描述】:

我正在关注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。但它似乎没有任何效果。文字看起来是白色的,带有绿色的光芒。我想要主颜色=颜色和发光=绿色的文本。

我该怎么办?

【问题讨论】:

    标签: iphone ios calayer glow


    【解决方案1】:

    我对你的颜色感到困惑......我认为在 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 ];

    【讨论】:

    • Albert 关于颜色是正确的。您需要将其设置为小于 1.0。此外,您的果岭后面需要有一个 f。如果你计算自己的值,它需要是格式 0.###f.
    • 感谢您的确认:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多