-(void)drawRect:(CGRect)rect
{
CGFloatR=0.0f,G=0.0f,B=0.0f;
CGContextRefctx=UIGraphicsGetCurrentContext();
CGSizefontSize=[self.currentTitlesizeWithFont:self.titleLabel.font
forWidth:self.bounds.size.width
lineBreakMode:UILineBreakModeTailTruncation];
UIColor*uiColor=self.titleLabel.textColor;
CGColorRefcolor=[uiColorCGColor];
intnumComponents=CGColorGetNumberOfComponents(color);
if(numComponents==4)
{
constCGFloat*components=CGColorGetComponents(color);
R=components[0];
G=components[1];
B=components[2];
}
CGContextSetRGBStrokeColor(ctx,R,G,B,1.0f);
CGContextSetLineWidth(ctx,1.0f);
CGPointl=CGPointMake(self.frame.size.width/2.0-fontSize.width/2.0,
self.frame.size.height/2.0+fontSize.height/2.0);
CGPointr=CGPointMake(self.frame.size.width/2.0+fontSize.width/2.0,
self.frame.size.height/2.0+fontSize.height/2.0);
CGContextMoveToPoint(ctx,l.x,l.y);
CGContextAddLineToPoint(ctx,r.x,r.y);
CGContextStrokePath(ctx);
[superdrawRect:rect];
}
通过重写drawRect方法,利用Core Graphics设置RGB颜色和线条宽度,为Button的文字添加下划线。首先获取文字颜色的RGB值,然后在当前图形上下文中设置颜色和线条宽度,并绘制从左到右的直线。最后调用父类的drawRect方法完成绘制。
3182

被折叠的 条评论
为什么被折叠?



