iOS开发:自定义带下划线文本的UIButton
Uiunderlinedbutton.h代码
- @interface UIUnderlinedButton : UIButton {
- }
- + (UIUnderlinedButton *) underlinedButton;
- @end
Uiunderlinedbutton.m代码
- @implementation UIUnderlinedButton
- + (UIUnderlinedButton*) underlinedButton {
- UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
- return [button autorelease];
- }
- - (void) drawRect:(CGRect)rect {
- CGRect textRect = self.titleLabel.frame;
- // need to put the line at top of descenders (negative value)
- CGFloat descender = self.titleLabel.font.descender;
- CGContextRef contextRef = UIGraphicsGetCurrentContext();
- // set to same colour as text
- CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);
- CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);
- CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
- CGContextClosePath(contextRef);
- CGContextDrawPath(contextRef, kCGPathStroke);
- }
- @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。