(iOS笔记)iOS上实现空心字体的两种方式

1、使用NSAttributedString

NSAttributedString *aStr = [[NSAttributedString alloc] initWithString:@"邪魔退散" attributes:@{NSStrokeWidthAttributeName:@2, NSStrokeColorAttributeName:[UIColor orangeColor]}];

self.demoLabel.attributedText = aStr;

这样的效果不太好看:


2、使用CoreGraphics


新建一个继承自UILabel的类,重写- (void)drawRect:(CGRect)rect方法

- (void)drawRect:(CGRect)rect {

     CGContextRef context = UIGraphicsGetCurrentContext();

     CGContextSetLineWidth(context, 2.5);

     CGContextSetLineJoin(context, kCGLineJoinRound);

     CGContextSetTextDrawingMode(context, kCGTextStroke);

     self.textColor = [UIColor orangeColor];

     //橙色空心字

     [super drawTextInRect:rect];

     CGContextSetTextDrawingMode(context, kCGTextFill);

     self.textColor = [UIColor whiteColor];

     //白色实心字

     [super drawTextInRect:rect];

}

先画一个空心字,然后在画一个实心字。两个字放一起产生的效果,好处就是可以自己定字体的粗细:

人生中第一个笔记结束------------------------------------------------END

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容