├── pic.gif ├── README.md ├── HyperlinksButton.h └── HyperlinksButton.m /pic.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaoyuan899/HyperlinksButton/HEAD/pic.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HyperlinksButton 2 | ================ 3 | ### 给UIButton 的Title 画下划线。 4 | draw a underline for UIButton title 5 | 6 | ### How To Use 7 | 将需要加下划线的Button类名修改为HyperlinksButton即可。 8 | setColor: 可以设置下划线颜色。 9 | ### 示例图片 10 | ![alt text][id] 11 | [id]:./pic.gif "Title" 12 | -------------------------------------------------------------------------------- /HyperlinksButton.h: -------------------------------------------------------------------------------- 1 | // 2 | // HyperlinksButton.h 3 | // memberapp 4 | // 5 | // Created by sam on 4/30/14. 6 | // Copyright (c) 2014 com.by-health. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface HyperlinksButton : UIButton 12 | { 13 | UIColor *lineColor; 14 | } 15 | -(void)setColor:(UIColor*)color; 16 | @end 17 | -------------------------------------------------------------------------------- /HyperlinksButton.m: -------------------------------------------------------------------------------- 1 | // 2 | // HyperlinksButton.m 3 | // memberapp 4 | // 5 | // Created by sam on 4/30/14. 6 | // Copyright (c) 2014 com.by-health. All rights reserved. 7 | // 8 | 9 | #import "HyperlinksButton.h" 10 | 11 | @implementation HyperlinksButton 12 | 13 | - (id)initWithFrame:(CGRect)frame 14 | { 15 | self = [super initWithFrame:frame]; 16 | if (self) { 17 | 18 | } 19 | return self; 20 | } 21 | 22 | -(void)setColor:(UIColor *)color{ 23 | lineColor = [color copy]; 24 | [self setNeedsDisplay]; 25 | } 26 | 27 | 28 | - (void) drawRect:(CGRect)rect { 29 | CGRect textRect = self.titleLabel.frame; 30 | CGContextRef contextRef = UIGraphicsGetCurrentContext(); 31 | 32 | CGFloat descender = self.titleLabel.font.descender; 33 | if([lineColor isKindOfClass:[UIColor class]]){ 34 | CGContextSetStrokeColorWithColor(contextRef, lineColor.CGColor); 35 | } 36 | 37 | CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender+1); 38 | CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender+1); 39 | 40 | CGContextClosePath(contextRef); 41 | CGContextDrawPath(contextRef, kCGPathStroke); 42 | } 43 | @end 44 | --------------------------------------------------------------------------------