头文件
#import <UIKit/UIKit.h> @interface UIStrokeLabel:UILabel - (void)setStrokeColor:(UIColor*)strokeColor width:(CGFloat)width; @end
实现文件
#import "UIStrokeLabel.h" @interface UIStrokeLabel () { UIColor *strokeColor_; CGFloat strokeWidth_; } @end @implementation UIStrokeLabel #pragma mark - initlize - (id)init { if(self= [super init]){ strokeColor_ = [[UIColor whiteColor] retain]; strokeWidth_ = 1.0f; } return self; } - (id)initWithFrame:(CGRect)frame { if(self= [super initWithFrame:frame]){ strokeColor_ = [[UIColor whiteColor] retain]; strokeWidth_ = 1.0f; } return self; } - (id)initWithCoder:(NSCoder*)aDecoder { if(self= [super initWithCoder:aDecoder]){ strokeColor_= [[UIColorwhiteColor] retain]; strokeWidth_= 1.0f; } return self; } #pragma mark - stroke - (void)setStrokeColor:(UIColor*)strokeColor width:(CGFloat)width { [strokeColor_ release]; strokeColor_ = nil; strokeColor_ = [strokeColor retain]; strokeWidth_ = width; [self setNeedsDisplay]; } - (void)drawTextInRect:(CGRect)rect { CGSize shadowOffset = self.shadowOffset; UIColor* textColor = self.textColor; CGContextRefcontext = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, strokeWidth_); CGContextSetLineJoin(context, kCGLineJoinRound); CGContextSetTextDrawingMode(context, kCGTextStroke); self.textColor = strokeColor_; [super drawTextInRect:rect]; CGContextSetTextDrawingMode(context, kCGTextFill); self.textColor = textColor; self.shadowOffset = CGSizeMake(0.0f, 0.0f); [super drawTextInRect:rect]; self.shadowOffset= shadowOffset; } - (void)dealloc { [strokeColor_ release]; [super dealloc]; } @end