forked from dzenbot/DZNSegmentedControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDZNSegmentedControl.m
More file actions
466 lines (363 loc) · 13.7 KB
/
DZNSegmentedControl.m
File metadata and controls
466 lines (363 loc) · 13.7 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// DZNSegmentedControl.m
// DZNSegmentedControl
// https://github.com/dzenbot/DZNSegmentedControl
//
// Created by Ignacio Romero Zurbuchen on 3/4/14.
// Copyright (c) 2014 DZN Labs. All rights reserved.
// Licence: MIT-Licence
//
#import "DZNSegmentedControl.h"
@interface DZNSegmentedControl ()
@property (nonatomic) BOOL initializing;
@property (nonatomic, strong) UIView *selectionIndicator;
@property (nonatomic, strong) UIView *hairline;
@property (nonatomic, getter = isTransitioning) BOOL transitioning;
@end
@implementation DZNSegmentedControl
@synthesize items = _items;
@synthesize selectedSegmentIndex = _selectedSegmentIndex;
@synthesize barPosition = _barPosition;
- (id)init
{
_initializing = YES;
if (self = [super init]) {
_selectedSegmentIndex = -1;
_font = [UIFont systemFontOfSize:1];
_height = 56.0;
_selectionIndicatorHeight = 2.0;
_animationDuration = 0.2;
_displayCount = YES;
_autoAdjustSelectionIndicatorWidth = YES;
_selectionIndicator = [UIView new];
[self addSubview:_selectionIndicator];
_hairline = [UIView new];
_hairline.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_hairline];
self.backgroundColor = [UIColor whiteColor];
}
_initializing = NO;
return self;
}
- (id)initWithItems:(NSArray *)items
{
if (self = [self init]) {
self.items = items;
}
return self;
}
#pragma mark - UIView Methods
- (CGSize)sizeThatFits:(CGSize)size
{
return CGSizeMake(self.superview.frame.size.width, _height);
}
- (void)sizeToFit
{
CGRect rect = self.frame;
rect.size = [self sizeThatFits:rect.size];
self.frame = rect;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self sizeToFit];
if ([self buttons].count == 0) {
_selectedSegmentIndex = -1;
}
else if (_selectedSegmentIndex < 0) {
_selectedSegmentIndex = 0;
}
for (int i = 0; i < [self buttons].count; i++) {
UIButton *button = [[self buttons] objectAtIndex:i];
[button setFrame:CGRectMake(roundf(self.frame.size.width/self.numberOfSegments)*i, 0, roundf(self.frame.size.width/self.numberOfSegments), self.frame.size.height)];
CGFloat topInset = (_barPosition > UIBarPositionBottom) ? -4.0 : 4.0;
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, topInset, 0)];
if (i == _selectedSegmentIndex) {
button.selected = YES;
}
}
_selectionIndicator.frame = [self selectionIndicatorRect];
_hairline.frame = [self hairlineRect];
[self bringSubviewToFront:_selectionIndicator];
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[super willMoveToSuperview:newSuperview];
[self layoutIfNeeded];
}
- (void)didMoveToWindow
{
if (!self.backgroundColor) {
self.backgroundColor = [UIColor whiteColor];
}
}
#pragma mark - Getter Methods
- (NSUInteger)numberOfSegments
{
return _items.count;
}
- (NSArray *)buttons
{
NSMutableArray *_buttons = [NSMutableArray new];
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
[_buttons addObject:view];
}
}
return _buttons;
}
- (UIButton *)buttonAtIndex:(NSUInteger)segment
{
if (_items.count > 0 && segment < [self buttons].count) {
return (UIButton *)[[self buttons] objectAtIndex:segment];
}
return nil;
}
- (UIButton *)selectedButton
{
if (_selectedSegmentIndex >= 0) {
return [self buttonAtIndex:_selectedSegmentIndex];
}
return nil;
}
- (NSString *)stringForSegmentAtIndex:(NSUInteger)segment
{
UIButton *button = [self buttonAtIndex:segment];
return [[button attributedTitleForState:UIControlStateNormal] string];
}
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment
{
if (_displayCount) {
NSString *title = [self stringForSegmentAtIndex:segment];
NSArray *components = [title componentsSeparatedByString:@"\n"];
return [components objectAtIndex:1];
}
return [self stringForSegmentAtIndex:segment];
}
- (NSNumber *)countForSegmentAtIndex:(NSUInteger)segment
{
NSString *title = [self stringForSegmentAtIndex:segment];
NSArray *components = [title componentsSeparatedByString:@"\n"];
return @([[components firstObject] intValue]);
}
- (CGRect)selectionIndicatorRect
{
UIButton *button = [self selectedButton];
NSString *title = [self titleForSegmentAtIndex:button.tag];
CGRect frame = _selectionIndicator.frame;
frame.origin.y = (_barPosition > UIBarPositionBottom) ? 0.0 : (button.frame.size.height-frame.size.height);
if (_autoAdjustSelectionIndicatorWidth) {
frame.size = CGSizeMake([title sizeWithAttributes:nil].width, _selectionIndicatorHeight);
frame.origin.x = (button.frame.size.width*(_selectedSegmentIndex))+(button.frame.size.width-frame.size.width)/2;
} else {
frame.size = CGSizeMake(button.frame.size.width, _selectionIndicatorHeight);
frame.origin.x = (button.frame.size.width*(_selectedSegmentIndex));
}
return frame;
}
- (UIColor *)hairlineColor
{
return _hairline.backgroundColor;
}
- (CGRect)hairlineRect
{
CGRect frame = CGRectMake(0, 0, self.frame.size.width, 0.5);
frame.origin.y = (_barPosition > UIBarPositionBottom) ? 0 : self.frame.size.height;
return frame;
}
#pragma mark - Setter Methods
- (void)setTintColor:(UIColor *)color
{
if (!color || !_items || _initializing) {
return;
}
[super setTintColor:color];
[self setTitleColor:color forState:UIControlStateHighlighted];
[self setTitleColor:color forState:UIControlStateSelected];
}
- (void)setItems:(NSArray *)items
{
if (_items) {
[self removeAllSegments];
}
if (items) {
_items = [NSArray arrayWithArray:items];
[self configure];
}
}
- (void)setDelegate:(id<DZNSegmentedControlDelegate>)delegate
{
_delegate = delegate;
_barPosition = [delegate positionForBar:self];
}
- (void)setSelectedSegmentIndex:(NSInteger)segment
{
if (segment > self.numberOfSegments-1) {
segment = 0;
}
[self setSelected:YES forSegmentAtIndex:segment];
}
- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment
{
if (!title) {
return;
}
NSAssert(segment >= 0, @"Cannot assign a title to a negative segment.");
NSMutableArray *items = [NSMutableArray arrayWithArray:self.items];
if (segment >= self.numberOfSegments) {
[items insertObject:title atIndex:self.numberOfSegments];
_items = items;
[self addButtonForSegment:segment];
}
else {
[items replaceObjectAtIndex:segment withObject:title];
_items = items;
[self setCount:[self countForSegmentAtIndex:segment] forSegmentAtIndex:segment];
}
}
- (void)setCount:(NSNumber *)count forSegmentAtIndex:(NSUInteger)segment
{
if (!count || !_items) {
return;
}
NSAssert(segment < self.numberOfSegments, @"Cannot assign a count to non-existing segment.");
NSAssert(segment >= 0, @"Cannot assign a title to a negative segment.");
NSString *text = [NSString stringWithFormat:@"%@\n%@", count ,[_items objectAtIndex:segment]];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSString* title;
if (_displayCount) {
title = [NSString stringWithFormat:@"%@\n%@", count ,[_items objectAtIndex:segment]];
}
else {
title = [NSString stringWithFormat:@"%@",[_items objectAtIndex:segment]];
}
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:title];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:_font.fontName size:19.0] range:[title rangeOfString:[count stringValue]]];
UIButton *button = [self buttonAtIndex:segment];
[button setAttributedTitle:attributedString forState:UIControlStateNormal];
[button setAttributedTitle:attributedString forState:UIControlStateHighlighted];
[button setAttributedTitle:attributedString forState:UIControlStateSelected];
[button setAttributedTitle:attributedString forState:UIControlStateDisabled];
[self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[self setTitleColor:self.tintColor forState:UIControlStateHighlighted];
[self setTitleColor:self.tintColor forState:UIControlStateSelected];
[self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
}
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
{
for (UIButton *button in [self buttons]) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitleForState:state]];
NSString *string = attributedString.string;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentCenter;
style.lineBreakMode = NSLineBreakByWordWrapping;
style.minimumLineHeight = 16.0;
NSArray *components = [attributedString.string componentsSeparatedByString:@"\n"];
NSString *count = [components objectAtIndex:0];
NSString *title = [components objectAtIndex:1];
[attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:_font.fontName size:19.0] range:[string rangeOfString:count]];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:_font.fontName size:12.0] range:[string rangeOfString:title]];
if (_displayCount) {
if (state == UIControlStateNormal) {
[attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, count.length)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[color colorWithAlphaComponent:0.5] range:NSMakeRange(count.length, title.length+1)];
}
else {
[attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, string.length)];
if (state == UIControlStateSelected) {
_selectionIndicator.backgroundColor = color;
}
}
} else {
[attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, attributedString.string.length)];
}
[button setAttributedTitle:attributedString forState:state];
}
}
- (void)setSelected:(BOOL)selected forSegmentAtIndex:(NSUInteger)segment
{
if (_selectedSegmentIndex == segment || self.isTransitioning) {
return;
}
for (UIButton *_button in [self buttons]) {
_button.highlighted = NO;
_button.selected = NO;
_button.userInteractionEnabled = YES;
}
CGFloat duration = (_selectedSegmentIndex < 0) ? 0.0 : _animationDuration;
_selectedSegmentIndex = segment;
_transitioning = YES;
UIButton *button = [self buttonAtIndex:segment];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut
animations:^{
_selectionIndicator.frame = [self selectionIndicatorRect];
}
completion:^(BOOL finished) {
button.userInteractionEnabled = NO;
_transitioning = NO;
}];
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment
{
UIButton *button = [self buttonAtIndex:segment];
button.enabled = enabled;
}
- (void)setHairlineColor:(UIColor *)color
{
if (_initializing) {
return;
}
_hairline.backgroundColor = color;
}
#pragma mark - DZNSegmentedControl Methods
- (void)configure
{
for (int i = 0; i < self.numberOfSegments; i++) {
[self addButtonForSegment:i];
}
}
- (void)addButtonForSegment:(NSUInteger)segment
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(willSelectedButton:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(didSelectedButton:) forControlEvents:UIControlEventTouchDragOutside|UIControlEventTouchDragInside|UIControlEventTouchDragEnter|UIControlEventTouchDragExit|UIControlEventTouchCancel|UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
button.backgroundColor = nil;
button.opaque = YES;
button.clipsToBounds = YES;
button.titleLabel.numberOfLines = 2;
button.adjustsImageWhenHighlighted = NO;
button.adjustsImageWhenDisabled = NO;
button.exclusiveTouch = YES;
button.tag = segment;
[self addSubview:button];
[self setCount:@(0) forSegmentAtIndex:segment];
[self layoutIfNeeded];
}
- (void)willSelectedButton:(id)sender
{
UIButton *button = (UIButton *)sender;
if (!self.isTransitioning) {
self.selectedSegmentIndex = button.tag;
}
}
- (void)didSelectedButton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.highlighted = NO;
button.selected = YES;
}
- (void)removeAllSegments
{
if (self.isTransitioning) {
return;
}
for (UIButton *_button in [self buttons]) {
[_button removeFromSuperview];
}
_items = nil;
}
@end