(iOS筆記)iOS上實現空心字體的兩種方式

分類:技術 時間:2017-01-13

1、使用NSAttributedString

NSAttributedString *aStr = [[NSAttributedString alloc] initWithString:@quot;邪魔退散quot; 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


Tags: iOS開發

文章來源:http://www.jianshu.com/p/8c3a6b55ffc2


ads
ads

相關文章
ads

相關文章

ad