码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 给UILabel字体加一个带颜色的边框

时间:2017-12-23 20:21:39      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:ado   ati   elf   简单实现   creat   color   post   current   字体   

今天做项目碰见一个UI效果,给字体加一圈白边,看起来就像是加了一个背景,思路就是继承一个UILabel,重新覆写drawTextInRect方法,就可以简单实现这个效果,上代码:

 1 //
 2 //  HGLLabel.m
 3 //  xxxxxxxxxx.xxx
 4 //
 5 //  Created by xxxxx on 2017/12/23.
 6 //  Copyright ? 2017年 xxxx. All rights reserved.
 7 //
 8 
 9 #import "HGLLabel.h"
10 
11 @implementation HGLLabel
12 
13 - (void)drawTextInRect:(CGRect)rect {
14     CGSize shadowOffset = self.shadowOffset;
15     UIColor *textColor = self.textColor;
16     
17     CGContextRef c = UIGraphicsGetCurrentContext();
18     CGContextSetLineWidth(c, 8.0);//字体边缘的宽度
19     CGContextSetLineJoin(c, kCGLineJoinRound);
20     
21     CGContextSetTextDrawingMode(c, kCGTextStroke);
22     self.textColor = [UIColor whiteColor];//字体边缘加的颜色
23     [super drawTextInRect:rect];
24     
25     CGContextSetTextDrawingMode(c, kCGTextFill);
26     self.textColor = textColor;
27     self.shadowOffset = CGSizeMake(0, 0);
28     [super drawTextInRect:rect];
29     
30     self.shadowOffset = shadowOffset;
31 }
32 
33 @end

 

 

注释很清楚,不多说。

IOS 给UILabel字体加一个带颜色的边框

标签:ado   ati   elf   简单实现   creat   color   post   current   字体   

原文地址:http://www.cnblogs.com/devfan/p/8093995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!