-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTopicHotkeyView.m
131 lines (101 loc) · 5.39 KB
/
TopicHotkeyView.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
//
// TopicHotkeyView.m
// Coding_iOS
//
// Created by pan Shiyu on 15/7/15.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#import "TopicHotkeyView.h"
#define kFirst_Hotkey_Color @"0x0060FF"
#define kOther_Hotkey_Text_Color @"0x222222"
#define kOther_HotKey_Border_Color @"0xb5b5b5"
@interface TopicHotkeyView ()
@property (nonatomic,strong) NSMutableArray *keyDatalist;
@property (nonatomic,strong) NSMutableArray *keyViewlist;
@end
@implementation TopicHotkeyView
- (void)setHotkeys:(NSArray *)hotkeys {
if (!_keyDatalist) {
_keyDatalist = [@[] mutableCopy];
_keyViewlist = [@[] mutableCopy];
}
for (UIView *subView in _keyViewlist) {
[subView removeFromSuperview];
}
[_keyViewlist removeAllObjects];
[_keyDatalist removeAllObjects];
if ([hotkeys count] <= 0) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, kScreen_Width, 0);
return;
}
[_keyDatalist addObjectsFromArray:hotkeys];
if(hotkeys.count) {
CGFloat currentWidth = 10.0f;
CGFloat currentHeight = 15.0f;
CGSize currentSize = CGSizeZero;
CGFloat maxWidth = kScreen_Width - 20.0f;
UIFont *hotkeyFont = [UIFont systemFontOfSize:12.0f];
for (int i = 0; i < _keyDatalist.count; i++) {
NSString *displayHotkey = [NSString stringWithFormat:@" #%@# ", hotkeys[i][@"name"]];
UIButton *btnHotkey = [UIButton buttonWithType:UIButtonTypeCustom];
[btnHotkey setTag:i];
[btnHotkey setTitle:displayHotkey forState:UIControlStateNormal];
[btnHotkey.titleLabel setFont:hotkeyFont];
btnHotkey.layer.borderWidth = 1.0f;
btnHotkey.layer.cornerRadius = 15.0f;
[btnHotkey addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
if(i == 0) {
[btnHotkey setTitleColor:[UIColor colorWithHexString:kFirst_Hotkey_Color] forState:UIControlStateNormal];
[btnHotkey setTitleColor:[UIColor colorWithHexString:kFirst_Hotkey_Color andAlpha:0.3] forState:UIControlStateHighlighted];
btnHotkey.layer.borderColor = [[UIColor colorWithHexString:kFirst_Hotkey_Color] CGColor];
}else {
[btnHotkey setTitleColor:[UIColor colorWithHexString:kOther_Hotkey_Text_Color] forState:UIControlStateNormal];
[btnHotkey setTitleColor:[UIColor colorWithHexString:kOther_Hotkey_Text_Color andAlpha:0.3] forState:UIControlStateHighlighted];
btnHotkey.layer.borderColor = [[UIColor colorWithHexString:kOther_HotKey_Border_Color] CGColor];
}
currentSize = [self computeSizeFromString:displayHotkey];
// currentSize = CGSizeMake(currentSize.width, currentSize.height + 10.0f);
if (currentSize.width >= maxWidth) {
CGFloat height = ((int)currentSize.width % (int)maxWidth ? currentSize.width / maxWidth : currentSize.width / maxWidth + 1) * currentSize.height;
btnHotkey.frame = CGRectMake(currentWidth, currentHeight, maxWidth, height);
currentHeight = currentHeight + height + 10.0f;
currentWidth = 10.0f;
}else {
if(currentSize.width + currentWidth < maxWidth) {
btnHotkey.frame = CGRectMake(currentWidth, currentHeight, currentSize.width, currentSize.height);
currentWidth = currentWidth + currentSize.width + 5.0f;
}else if (currentSize.width + currentWidth == maxWidth) {
btnHotkey.frame = CGRectMake(currentWidth, currentHeight, currentSize.width, currentSize.height);
currentWidth = 10.0f;
currentHeight = currentHeight + currentSize.height + 10.0f;
}else if(currentSize.width + currentWidth > maxWidth ) {
currentWidth = 10.0f;
currentHeight = currentHeight + currentSize.height + 10.0f;
btnHotkey.frame = CGRectMake(currentWidth, currentHeight, currentSize.width, currentSize.height);
currentWidth = currentWidth + currentSize.width + 5.0f;
}
}
if(i == hotkeys.count - 1) {
currentHeight += currentSize.height + 15.0f;
}
[self addSubview:btnHotkey];
[_keyViewlist addObject:btnHotkey];
}
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, kScreen_Width, currentHeight);
}
}
- (CGSize)computeSizeFromString:(NSString *)string {
CGSize maxSize = CGSizeMake(kScreen_Width, 30);
CGSize curSize = [string boundingRectWithSize:maxSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:12.0f] }
context:nil].size;
return CGSizeMake(ceilf(curSize.width), 30);
}
- (void)didClickButton:(id)sender {
NSInteger index = [(UIButton *)sender tag];
if (_block) {
_block(_keyDatalist[index]);
}
}
@end