自定义UIlabel来实现。
在.h文件中添加属性
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
/// 标准文字外描边
@interface MGPGLabel : UILabel
/// 外描边颜色
@property (strong,nonatomic) UIColor *strokeColor;
/// 外描边宽度
@property (assign,nonatomic) CGFloat strokeWidth;
@end
NS_ASSUME_NONNULL_END
然后在.m文件中 重写drawTextInRect 方法
#import "MGPGLabel.h"
@implementation MGPGLabel
- (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];
}
}
@end
网友评论