iOS開(kāi)發(fā):自定義帶下劃線文本的UIButton
作者:佚名
本文為各位51CTO的網(wǎng)友朋友們介紹了iOS開(kāi)發(fā)實(shí)戰(zhàn)中如何自定義帶下劃線文本的UIButton,文章比較簡(jiǎn)潔,直接將Uiunderlinedbutton.h和Uiunderlinedbutton.m的代碼整理出來(lái),各位讀者可以親自實(shí)現(xiàn)一邊這些代碼,在慢慢消化了解。
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
責(zé)任編輯:閆佳明
來(lái)源:
apkbus