ios lable字体描边+外发光

2021-07-26  本文已影响0人  guoguojianshu

继承label的一个类,实现

@interface custom : UILabel
@property (strong,nonatomic) UIColor *strokeColor;
@property (assign,nonatomic) CGFloat strokeWidth;
@end
@implementation custom

- (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

使用方法

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    custom * label = [custom new];
    label.frame = CGRectMake(160, 70, 150, 100);
    label.text = @"Hello";
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor greenColor];
    label.font = [UIFont systemFontOfSize:30];
    //描边
    label.strokeColor = [UIColor orangeColor];
    label.strokeWidth = 1;
    //发光
    label.layer.shadowRadius = 10;
    label.layer.shadowColor = [UIColor colorWithRed:230/255.0 green:75/255.0 blue:248/255.0 alpha:1.0].CGColor;
    label.layer.shadowOffset = CGSizeMake(0, 0);
    label.layer.shadowOpacity = 1.0;
    [self.view addSubview:label];
    
}
上一篇 下一篇

猜你喜欢

热点阅读