實現UILable字型即粗體又有描邊
阿新 • • 發佈:2019-01-27
#import <UIKit/UIKit.h>
@interface LMEStrokeLabel : UILabel
/** 描多粗的邊*/
@property (nonatomic, assign) NSInteger strokeWidth;
/** 外輪顏色*/
@property (nonatomic, strong) UIColor *strokeTextColor;
/** 裡面字型預設顏色*/
@property (nonatomic, strong) UIColor *labelTextColor;
@end
#import "LMEStrokeLabel.h"
@implementation LMEStrokeLabel
- (void)drawTextInRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, self.strokeWidth);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = self .strokeTextColor;
[super drawTextInRect:rect];
CGFontRef font = CGFontCreateWithFontName((__bridge CFStringRef)self.font.fontName);
CGContextSetFont(c, font);
self.textColor = self.labelTextColor;
CGContextSetTextDrawingMode(c, kCGTextFill);
[super drawTextInRect:rect];
}
@end