-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomSectionIndex.m
More file actions
107 lines (65 loc) · 3.13 KB
/
Copy pathCustomSectionIndex.m
File metadata and controls
107 lines (65 loc) · 3.13 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
//
// CustomSectionIndex.m
//
// Created by Javi on 14/12/12.
// Copyright (c) 2012 Javi. All rights reserved.
//
#import "CustomSectionIndex.h"
@implementation CustomSectionIndex
- (id)init {
_sectionIndexFont = [UIFont fontWithName: @"HelveticaNeue-Bold" size: 9];
UIImage *backgroundImage = [[UIImage imageNamed: @"IndexTableBackground.png"] resizableImageWithCapInsets: UIEdgeInsetsMake(18, 20, 18, 20)];
_indexArray = [NSMutableArray arrayWithObjects: @"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"Ñ",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
// Calculamos cuánto ocupa cada letra para calcular el tamaño total
letterSize = [[_indexArray lastObject] sizeWithFont: _sectionIndexFont];
// El alto será tantas veces el tamaño de una letra por las letras totales.
self = [super initWithFrame: CGRectMake(0, 0, backgroundImage.size.width, (letterSize.height * [_indexArray count]) + 8)];
if (self) {
sectionIndexBackground = [[UIImageView alloc] initWithFrame: self.frame];
sectionIndexBackground.image = backgroundImage;
[self addSubview: sectionIndexBackground];
[self createInitialData];
}
return self;
}
- (void) createInitialData {
float init = 4;
int tag = 1;
for (NSString *letter in _indexArray) {
UILabel *letterLabel = [[UILabel alloc] initWithFrame: CGRectMake(4, init, self.frame.size.width - 8, letterSize.height)];
letterLabel.font = _sectionIndexFont;
letterLabel.textColor = [UIColor colorWithRed:106/255.0 green:115/255.0 blue:125/255.0 alpha:1.0];
letterLabel.backgroundColor = [UIColor clearColor];
letterLabel.textAlignment = UITextAlignmentCenter;
letterLabel.text = letter;
letterLabel.userInteractionEnabled = YES;
letterLabel.tag = tag++;
[self addSubview: letterLabel];
init += letterLabel.frame.size.height;
}
}
#pragma mark - UIResponder methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: self];
[self getSectionTouched: location];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: self];
[self getSectionTouched: location];
}
#pragma mark - Other methods
// Calculamos la sección en la que hemos tocado
- (void) getSectionTouched: (CGPoint) touched {
int sectionTouched = -1;
if (touched.x > 4 && touched.x < 24 && touched.y > 4 && touched.y < (self.frame.size.height-8) ) {
sectionTouched = (touched.y - 5) / letterSize.height;
}
if (sectionTouched != -1) {
[_delegate customSectionIndex:self withNewSection: sectionTouched andLetter: [_indexArray objectAtIndex: sectionTouched]];
} else {
[_delegate customSectionIndex:self withNewSection: sectionTouched andLetter: [NSString string]];
}
}
@end