开发者

Creating a stroke and shadow affect using only 1 UILabel subclass

开发者 https://www.devze.com 2023-03-17 13:53 出处:网络
Im trying to create a UILabel subclass that will accomplish the task of the 2 separate UILabels i have stacked on top of each other using the following methods:

Im trying to create a UILabel subclass that will accomplish the task of the 2 separate UILabels i have stacked on top of each other using the following methods:

Background UILabel:

- (void)drawTextInRect:(CGRect)rect {

CGSize shadowOffset = self.shadowOffset;
//UIColor *textColor = self.textColor;

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 3);

CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];

self.alpha = .1;

self.shadowOffset = shadowOffset;

}

Foreground UILabel:

- (void)drawTextInRect:(CGRect)rect {

CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 2);

CGContextSetTextDrawingMode(c, kCG开发者_如何学GoTextStroke);
self.textColor = [UIColor blackColor];
[super drawTextInRect:rect];


CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];

self.alpha = .5;

self.shadowOffset = shadowOffset;

}

Is there a way to accomplish these 2 sets of affects using only 1 UILabel subclass?


Try this code:

UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(0,1);
0

精彩评论

暂无评论...
验证码 换一张
取 消