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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// GXFButtonAndLineView.m
// XFFruit
//
// Created by freecui on 15/9/4.
// Copyright (c) 2015年 Xummer. All rights reserved.
//
#import "GXFButtonAndLineView.h"
@implementation GXFButtonAndLineView
- (instancetype)initViewLineAndButtonWithFrame: (CGRect)frame
isSeleted: (BOOL)isSelected
buttonTitle: (NSString *)title
selectColor: (UIColor *)color {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, self.width, self.height - 1);
[btn setTitle:title forState:UIControlStateNormal];
CGSize labelSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: btn.titleLabel.font}];
UIView *line = [[UIView alloc]initWithFrame:CGRectMake((self.width - labelSize.width ) * 0.5, btn.bottom, labelSize.width, 1)];
if (isSelected) {
[btn setTitleColor:GXF_GREEN_COLOR forState:UIControlStateNormal];
line.backgroundColor = GXF_GREEN_COLOR;
} else {
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
line.backgroundColor = [UIColor whiteColor];
}
self.f_btn = btn;
self.f_line = line;
[self addSubview:line];
[self addSubview:btn];
return self;
}
- (instancetype)initViewLineAndButtonWithFrame: (CGRect)frame
isSeleted: (BOOL)isSelected
buttonTitle: (NSString *)title
selectColor: (UIColor *)color
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
{
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, self.width, self.height - 1);
[btn setTitle:title forState:UIControlStateNormal];
[btn addTarget:self action:action forControlEvents:controlEvents];
CGSize labelSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: btn.titleLabel.font}];
UIView *line = [[UIView alloc]initWithFrame:CGRectMake((self.width - labelSize.width ) * 0.5, btn.bottom, labelSize.width, 1)];
if (isSelected) {
[btn setTitleColor:GXF_GREEN_COLOR forState:UIControlStateNormal];
line.backgroundColor = GXF_GREEN_COLOR;
} else {
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
line.backgroundColor = [UIColor whiteColor];
}
self.f_btn = btn;
self.f_line = line;
[self addSubview:line];
[self addSubview:btn];
return self;
}
@end