-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathRFToolbarButton.m
71 lines (53 loc) · 2.07 KB
/
RFToolbarButton.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
61
62
63
64
65
66
67
68
69
70
71
//
// RFToolbarButton.m
//
// Created by Rudd Fawcett on 12/3/13.
// Copyright (c) 2013 Rudd Fawcett. All rights reserved.
//
#import "RFToolbarButton.h"
@interface RFToolbarButton ()
/**
* The button's title.
*/
@property (nonatomic, strong) NSString *title;
/**
* The button's event block.
*/
@property (nonatomic, copy) eventHandlerBlock buttonPressBlock;
@end
@implementation RFToolbarButton
+ (instancetype)buttonWithTitle:(NSString *)title {
return [[self alloc] initWithTitle:title];
}
+ (instancetype)buttonWithTitle:(NSString *)title andEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent {
RFToolbarButton *newButton = [RFToolbarButton buttonWithTitle:title];
[newButton addEventHandler:eventHandler forControlEvents:controlEvent];
return newButton;
}
- (id)initWithTitle:(NSString *)title {
_title = title;
return [self init];
}
- (id)init {
CGSize sizeOfText = [self.title sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14.f]}];
sizeOfText.width = MAX(sizeOfText.width, 20);
if (self = [super initWithFrame:CGRectMake(0, 0, sizeOfText.width + 18.104, sizeOfText.height + 10.298)]) {
self.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.0];
self.layer.cornerRadius = 3.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
[self setTitle:self.title forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.500 alpha:1.0] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14.f];
self.titleLabel.textColor = [UIColor colorWithWhite:0.500 alpha:1.0];
}
return self;
}
- (void)addEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent {
self.buttonPressBlock = eventHandler;
[self addTarget:self action:@selector(buttonPressed) forControlEvents:controlEvent];
}
- (void)buttonPressed {
self.buttonPressBlock();
}
@end