-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIUnderlinedButton.m
executable file
·60 lines (44 loc) · 1.96 KB
/
UIUnderlinedButton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// UIUnderlinedButton.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-4.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kUnderlinedButtonHeight 20.0
#import "UIUnderlinedButton.h"
@implementation UIUnderlinedButton
+ (UIUnderlinedButton*) underlinedButton {
UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
return button;
}
+ (UIUnderlinedButton *)buttonWithTitle:(NSString *)title andFont:(UIFont *)font andColor:(UIColor *)color{
UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
CGFloat titleWidth = [title getWidthWithFont:font constrainedToSize:CGSizeMake(kScreen_Width, kUnderlinedButtonHeight)];
button.frame = CGRectMake(0, 0, titleWidth + 20.0, kUnderlinedButtonHeight);
[button.titleLabel setFont:font];
[button setTitleColor:color forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
return button;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect textRect = self.titleLabel.frame;
// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender+2;
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);
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
[super setTitle:title forState:state];
[self setNeedsDisplay];
}
@end