重写drawrect,带下划线的button

通过重写drawRect方法,利用Core Graphics设置RGB颜色和线条宽度,为Button的文字添加下划线。首先获取文字颜色的RGB值,然后在当前图形上下文中设置颜色和线条宽度,并绘制从左到右的直线。最后调用父类的drawRect方法完成绘制。

-(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];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值