简体   繁体   English

IPhone文字发光效果

[英]IPhone Text Glow Effect

In my IPhone application, I want the text in UILabel to glow for a second, then fade for a sec;. 在我的iPhone应用程序中,我希望UILabel中的文本发光一秒钟,然后褪色一秒钟。 Also i want to repeat this cycle for say 3 or 4 times. 我也想重复这个循环说3到4次。

Is this possible? 这可能吗?

As of 3.2 you there is direct support for shadows in the SDK. 从3.2开始,您可以直接支持SDK中的阴影。

label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);

Play with the parameters: 玩参数:

label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;

And to avoid shadow being clipped by the label bouds: 并避免阴影被标签bouds剪切:

label.layer.masksToBounds = NO;

Don't forget to 别忘了

#include <Quartzcore/Quartzcore.h>

and link against the QuartzCore or CoreGraphics frameworks (thanks to commenters for pointing this out). 并链接QuartzCoreCoreGraphics框架(感谢评论者指出这一点)。

I've posted some sample code which subclasses UILabel and enables you to apply glow and soft shadows to text. 我发布了一些子类UILabel的示例代码,使您可以将发光和柔和阴影应用于文本。

http://www.redrobotstudios.com/blog/2010/04/29/create-glow-soft-shadow-text-on-iphone/ http://www.redrobotstudios.com/blog/2010/04/29/create-glow-soft-shadow-text-on-iphone/

- (UILabel *) setUpGlowLabelWithFrame: (CGRect) frame fontSize: (int)fontSize {
        UILabel* label = [[UILabel alloc] initWithFrame:frame];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:fontSize];
        label.textColor = [UIColor whiteColor];
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        label.textAlignment = UITextAlignmentCenter;
        label.layer.shadowColor = [label.textColor CGColor];
        label.layer.shadowOffset = CGSizeMake(0.0, 0.0);
        label.layer.masksToBounds = NO;

    label.layer.shadowRadius = 0.5f;
    label.layer.shadowOpacity = 0.95;
    label.numberOfLines = 2;
    label.tag = 20;

    return label;
}

I get the glow effect when using this. 使用它时我得到了发光效果。

Hope it helps. 希望能帮助到你。

Happy coding :) 快乐编码:)

Yes. 是。 Use beginAnimation...commitAnimation, and use the alpha value to brighten or dim the ULabel. 使用beginAnimation ... commitAnimation,并使用alpha值使ULabel变亮或变暗。 Make sure that the default value of the UILabel's alpha starts at 0.85 and brightens to 1.0 and then dims to 0.75, and when all is done, you go back to 0.85. 确保UILabel alpha的默认值从0.85开始并亮到1.0然后变暗到0.75,完成所有后,你回到0.85。

There are other ways to do it such as having another view on top of the label that is gray or black and you use the same begin...commitAnimation to change the alpha on that from 0 to 0.20 or so. 还有其他方法可以做到这一点,例如在标签顶部有一个灰色或黑色的另一个视图,并使用相同的begin ... commitAnimation将其上的alpha从0更改为0.20左右。

There are many ways to do this, with varying quality. 有很多方法可以做到这一点,质量参差不齐。 One way would be to subclass UILabel, and implement some kind of gradient effect in coregraphics in the drawRect method. 一种方法是子类化UILabel,并在drawRect方法的coregraphics中实现某种渐变效果。

You can also play with the text shadow (change the colour and alpha) and see if you can come up with a decent glow. 你也可以玩文字阴影(改变颜色和alpha),看看你是否能想出一个像样的发光。

The easiest way is probably to make a transparent glow-outline image in photoshop and place it behind your text, and then do like mahboudz suggests... fade the image in and out using coreanimation. 最简单的方法可能是在Photoshop中制作一个透明的发光轮廓图像并将其放在文本后面,然后像mahboudz建议的那样...使用coreanimation淡入淡出图像。

For those of you using Swift 4 , here's what I used for multiple objects to Glow the same color as they are: 对于那些使用Swift 4的人来说 ,这是我用于多个对象的颜色与它们相同的颜色:

let colorRed: UIColor? = timeLabel.textColor
timeLabel.layer.shadowColor = colorRed?.cgColor
timeLabel.layer.shadowRadius = 4.0
timeLabel.layer.shadowOpacity = 0.9
timeLabel.layer.shadowOffset = CGSize.zero
timeLabel.layer.masksToBounds = false

As for animating the glow, just add a timer for 3-4 loops and change .shadowOpacity to something lower. 至于动画效果,只需为3-4个循环添加一个计时器,并将.shadowOpacity更改为更低的值。

发光时间与发光戒指

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM