├── CMIndexBar.h ├── CMIndexBar.m ├── LICENSE └── README.md /CMIndexBar.h: -------------------------------------------------------------------------------- 1 | // 2 | // indexBar.h 3 | // 4 | // Created by Craig Merchant on 07/04/2011. 5 | // Copyright 2011 RaptorApps. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | @protocol CMIndexBarDelegate; 11 | 12 | @interface CMIndexBar : UIView 13 | 14 | - (id)init; 15 | - (id)initWithFrame:(CGRect)frame; 16 | - (void)setIndexes:(NSArray*)indexes; 17 | - (void)clearIndex; 18 | 19 | @property (nonatomic, weak) id delegate; 20 | @property (nonatomic, strong) UIColor *highlightedBackgroundColor; 21 | @property (nonatomic, strong) UIColor *textColor; 22 | @property (nonatomic, strong) UIFont *textFont; 23 | 24 | @end 25 | 26 | @protocol CMIndexBarDelegate 27 | @optional 28 | - (void)indexSelectionDidChange:(CMIndexBar *)indexBar index:(NSInteger)index title:(NSString*)title; 29 | @end -------------------------------------------------------------------------------- /CMIndexBar.m: -------------------------------------------------------------------------------- 1 | // 2 | // CMIndexBar.m 3 | // 4 | // Created by Craig Merchant on 07/04/2011. 5 | // 6 | 7 | #import "CMIndexBar.h" 8 | 9 | #import 10 | 11 | @interface CMIndexBar () 12 | 13 | - (void)initializeDefaults; 14 | 15 | @end 16 | 17 | @implementation CMIndexBar 18 | 19 | @synthesize delegate; 20 | @synthesize highlightedBackgroundColor; 21 | @synthesize textColor; 22 | 23 | - (id)init { 24 | if ((self = [super init])) { 25 | [self initializeDefaults]; 26 | } 27 | 28 | return self; 29 | } 30 | 31 | - (id)initWithFrame:(CGRect)frame { 32 | if ((self = [super initWithFrame:frame])) { 33 | [self initializeDefaults]; 34 | } 35 | 36 | return self; 37 | } 38 | 39 | - (void)initializeDefaults { 40 | // Default colors. 41 | self.backgroundColor = [UIColor clearColor]; 42 | self.textColor = [UIColor blackColor]; 43 | self.highlightedBackgroundColor = [UIColor lightGrayColor]; 44 | 45 | // HelveticaNeue Bold 11pt is a font of native table index 46 | self.textFont = [UIFont fontWithName: @"HelveticaNeue-Bold" size: 11]; 47 | } 48 | 49 | - (void)layoutSubviews 50 | { 51 | [super layoutSubviews]; 52 | 53 | int i=0; 54 | int subcount=0; 55 | 56 | for (UIView *subview in self.subviews) 57 | { 58 | if ([subview isKindOfClass:[UILabel class]]) 59 | { 60 | subcount++; 61 | } 62 | } 63 | 64 | for (UIView *subview in self.subviews) 65 | { 66 | if ([subview isKindOfClass:[UILabel class]]) 67 | { 68 | float ypos; 69 | 70 | if(i == 0) 71 | { 72 | ypos = 0; 73 | } 74 | else if(i == subcount-1) 75 | { 76 | ypos = self.frame.size.height-24.0; 77 | } 78 | else 79 | { 80 | float sectionheight = ((self.frame.size.height-24.0)/subcount); 81 | 82 | sectionheight = sectionheight+(sectionheight/subcount); 83 | 84 | ypos = (sectionheight*i); 85 | } 86 | 87 | [subview setFrame:CGRectMake(0, ypos, self.frame.size.width, 24.0)]; 88 | 89 | i++; 90 | } 91 | } 92 | } 93 | 94 | - (void) setIndexes:(NSArray*)indexes 95 | { 96 | [self clearIndex]; 97 | 98 | int i; 99 | 100 | for (i=0; i<[indexes count]; i++) 101 | { 102 | float ypos; 103 | 104 | if(i == 0) 105 | { 106 | ypos = 0; 107 | } 108 | else if(i == [indexes count]-1) 109 | { 110 | ypos = self.frame.size.height-24.0; 111 | } 112 | else 113 | { 114 | float sectionheight = ((self.frame.size.height-24.0)/[indexes count]); 115 | sectionheight = sectionheight+(sectionheight/[indexes count]); 116 | 117 | ypos = (sectionheight*i); 118 | } 119 | 120 | UILabel *alphaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ypos, self.frame.size.width, 24.0)]; 121 | alphaLabel.textAlignment = NSTextAlignmentCenter; 122 | alphaLabel.text = [indexes objectAtIndex:i]; 123 | alphaLabel.font = self.textFont; 124 | alphaLabel.backgroundColor = [UIColor clearColor]; 125 | alphaLabel.textColor = textColor; 126 | [self addSubview:alphaLabel]; 127 | } 128 | } 129 | 130 | - (void) clearIndex 131 | { 132 | for (UIView *subview in self.subviews) 133 | { 134 | [subview removeFromSuperview]; 135 | } 136 | } 137 | 138 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 139 | { 140 | [super touchesEnded:touches withEvent:event]; 141 | [self touchesEndedOrCancelled:touches withEvent:event]; 142 | } 143 | 144 | - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 145 | { 146 | [super touchesCancelled:touches withEvent:event]; 147 | [self touchesEndedOrCancelled:touches withEvent:event]; 148 | } 149 | 150 | - (void) touchesEndedOrCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 151 | UIView *backgroundView = (UIView*)[self viewWithTag:767]; 152 | [backgroundView removeFromSuperview]; 153 | } 154 | 155 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 156 | { 157 | [super touchesBegan:touches withEvent:event]; 158 | 159 | UIView *backgroundview = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.bounds.size.width, self.bounds.size.height)]; 160 | [backgroundview setBackgroundColor:highlightedBackgroundColor]; 161 | backgroundview.layer.cornerRadius = self.bounds.size.width/2; 162 | backgroundview.layer.masksToBounds = YES; 163 | backgroundview.tag = 767; 164 | [self addSubview:backgroundview]; 165 | [self sendSubviewToBack:backgroundview]; 166 | 167 | if (!self.delegate) return; 168 | 169 | CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self]; 170 | 171 | if(touchPoint.x < 0) 172 | { 173 | return; 174 | } 175 | 176 | NSString *title = nil; 177 | int count=0; 178 | 179 | for (UILabel *subview in self.subviews) 180 | { 181 | if ([subview isKindOfClass:[UILabel class]]) 182 | { 183 | if(touchPoint.y < subview.frame.origin.y+subview.frame.size.height) 184 | { 185 | count++; 186 | title = subview.text; 187 | break; 188 | } 189 | 190 | count++; 191 | title = subview.text; 192 | } 193 | } 194 | 195 | if ([delegate respondsToSelector: @selector(indexSelectionDidChange:index:title:)]) 196 | [delegate indexSelectionDidChange: self index: count - 1 title: title]; 197 | } 198 | 199 | 200 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 201 | { 202 | [super touchesMoved:touches withEvent:event]; 203 | 204 | if (!self.delegate) return; 205 | 206 | CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self]; 207 | 208 | if(touchPoint.x < 0) 209 | { 210 | return; 211 | } 212 | 213 | NSString *title = nil; 214 | int count=0; 215 | 216 | for (UILabel *subview in self.subviews) 217 | { 218 | if ([subview isKindOfClass:[UILabel class]]) 219 | { 220 | if(touchPoint.y < subview.frame.origin.y+subview.frame.size.height) 221 | { 222 | count++; 223 | title = subview.text; 224 | break; 225 | } 226 | 227 | count++; 228 | title = subview.text; 229 | } 230 | } 231 | 232 | if ([delegate respondsToSelector: @selector(indexSelectionDidChange:index:title:)]) 233 | [delegate indexSelectionDidChange: self index: count - 1 title: title]; 234 | } 235 | 236 | @end 237 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 by Craig Merchant 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple replacement for UITableView Index that allows setting of colors. 2 | ----------------------------------------------------------------------- 3 | 4 | **Now it supports ARC.** 5 | 6 | Usage example: 7 | 8 | ```Objective-C 9 | CMIndexBar *indexBar = [[CMIndexBar alloc] initWithFrame:CGRectMake(self.view.frame.size.width-35, 10.0, 28.0, self.view.frame.size.height-20)]; 10 | [indexBar setIndexes:[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil]]; 11 | [self.view addSubview:indexBar]; 12 | ``` 13 | 14 | Delegate: 15 | 16 | ```Objective-C 17 | - (void)indexSelectionDidChange:(CMIndexBar *)indexBar index:(NSInteger)index title:(NSString*)title; 18 | ``` 19 | 20 | ToDo: 21 | ----- 22 | 1. ~~Font changing support~~ 23 | 1. UITableViewIndexSearch glyph support 24 | 25 | 26 | --------------------------------------------------------------------------------