繼承l(wèi)abel的一個類澄港,實現(xiàn)
@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];
//畫內(nèi)文字
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;
//發(fā)光
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];
}