-
Notifications
You must be signed in to change notification settings - Fork 0
/
QBPopupMenuItemView.m
executable file
·146 lines (109 loc) · 3.54 KB
/
QBPopupMenuItemView.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// QBPopupMenuItemView.m
// QBPopupMenu
//
// Created by Tanaka Katsuma on 2013/11/22.
// Copyright (c) 2013年 Katsuma Tanaka. All rights reserved.
//
#import "QBPopupMenuItemView.h"
#import "QBPopupMenu.h"
#import "QBPopupMenuItem.h"
@interface QBPopupMenuItemView ()
@property (nonatomic, strong, readwrite) UIButton *button;
@end
@implementation QBPopupMenuItemView
+ (instancetype)itemViewWithItem:(QBPopupMenuItem *)item
{
return [[self alloc] initWithItem:item];
}
- (instancetype)initWithItem:(QBPopupMenuItem *)item
{
self = [super initWithFrame:CGRectZero];
if (self) {
// View settings
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
// Create button
self.button = ({
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = self.bounds;
button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[button addTarget:self action:@selector(performAction) forControlEvents:UIControlEventTouchUpInside];
// Set style
button.contentMode = UIViewContentModeScaleAspectFit;
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
button;
});
[self addSubview:self.button];
// Property settings
self.item = item;
}
return self;
}
#pragma mark - Accessors
- (void)setImage:(UIImage *)image
{
[self.button setBackgroundImage:image forState:UIControlStateNormal];
}
- (UIImage *)image
{
return [self.button backgroundImageForState:UIControlStateNormal];
}
- (void)setHighlightedImage:(UIImage *)highlightedImage
{
[self.button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
}
- (UIImage *)highlightedImage
{
return [self.button backgroundImageForState:UIControlStateHighlighted];
}
- (void)setItem:(QBPopupMenuItem *)item
{
_item = item;
// Update
[self configureButton];
}
#pragma mark - Actions
- (void)performAction
{
if (self.item.target && self.item.action) {
[self.item.target performSelector:self.item.action withObject:nil afterDelay:0];
}
// Close popup menu
[self.popupMenu dismissAnimated:YES];
}
#pragma mark - Updating the View
- (void)sizeToFit
{
CGSize size = [self sizeThatFits:CGSizeZero];
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize buttonSize = [self.button sizeThatFits:CGSizeZero];
buttonSize.width += 10 * 2;
return buttonSize;
}
- (void)configureButton
{
// Title
[self.button setTitle:self.item.title forState:UIControlStateNormal];
// Image
[self.button setImage:self.item.image forState:UIControlStateNormal];
[self.button setImage:self.item.image forState:UIControlStateHighlighted];
// Content edge insets
if (self.item.title && self.item.image) {
self.button.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
self.button.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0);
} else {
self.button.titleEdgeInsets = UIEdgeInsetsZero;
self.button.imageEdgeInsets = UIEdgeInsetsZero;
}
}
@end