继承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];
}
网友评论