1. 程式人生 > >iOS UILabel文字新增描邊實現

iOS UILabel文字新增描邊實現

可以達到文字描一圈黑邊的效果: 在這裡插入圖片描述

繼承UILabel以後過載drawTextInRect:

- (void)drawTextInRect:(CGRect)rect 
{    
	CGSize shadowOffset = self.shadowOffset;  
	UIColor *textColor = self.textColor;    
	
	CGContextRef c = UIGraphicsGetCurrentContext();   
	CGContextSetLineWidth(c, 1);   
	CGContextSetLineJoin(c, kCGLineJoinRound);    
	
	CGContextSetTextDrawingMode(c, kCGTextStroke);   
	self.textColor = [UIColor whiteColor];   
	[super drawTextInRect:rect];  
	
	CGContextSetTextDrawingMode(c, kCGTextFill);   
	self.textColor = textColor;   
	self.shadowOffset = CGSizeMake(0, 0);   
	[super drawTextInRect:rect];    
	
	self.shadowOffset = shadowOffset; 
}