【iOS UI篇】Label描边+发光字

本文介绍如何给Label实现酷炫的描边+外发光效果,虽然实现简单,但是网上资料却是很少。


绘制实现描边

继承Lable,重写下面方法
.h文件:

@property (strong,nonatomic) UIColor *strokeColor;
@property (assign,nonatomic) CGFloat strokeWidth;

.m文件

- (void)drawTextInRect:(CGRect)rect
{
    if (self.strokeWidth > 0) {
        CGSize shadowOffset = self.shadowOffset;
        UIColor *textColor = self.textColor;
    
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(c, self.strokeWidth);
        CGContextSetLineJoin(c, kCGLineJoinRound);
        //画外边
        CGContextSetTextDrawingMode(c, kCGTextStroke);
        self.textColor = self.strokeColor;
        [super drawTextInRect:rect];
        //画内文字
        CGContextSetTextDrawingMode(c, kCGTextFill);
        self.textColor = textColor;
        self.shadowOffset = CGSizeMake(0, 0);
        [super drawTextInRect:rect];
        self.shadowOffset = shadowOffset;
    } else {
        [super drawTextInRect:rect];
    }
}

使用方法

MSStrokeLabel *label = [MSStrokeLabel new];
label.frame = CGRectMake(160, 70, 150, 100);
label.text = @"Hello";
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor greenColor];
label.font = [UIFont systemFontOfSize:50];
//描边
label.strokeColor = [UIColor orangeColor];
label.strokeWidth = 1;
//发光
label.layer.shadowRadius = 2;
label.layer.shadowColor = [UIColor redColor].CGColor;
label.layer.shadowOffset = CGSizeMake(0, 0);
label.layer.shadowOpacity = 1.0;
[self.view addSubview:label];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,588评论 25 709
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,406评论 0 1
  • 这个城市对孤独上了瘾, 孤独对路边的行人下了毒, 既然温和的路灯无法给我拥抱, 那我就不知道能把你戒掉。 你的身体...
    小醉逸尘阅读 346评论 2 2
  • 每天要预留一个小时打码!
    玉米霞阅读 209评论 0 0
  • 起床7点 学英语口语,单词,新闻 I'll come see you every day. Be around s...
    Jane小白917阅读 275评论 0 0