SSCheckBoxView.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright 2011 Ahmet Ardal
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. //
  14. // SSCheckBoxView.m
  15. // SSCheckBoxView
  16. //
  17. // Created by Ahmet Ardal on 12/6/11.
  18. // Copyright 2011 SpinningSphere Labs. All rights reserved.
  19. //
  20. #import "SSCheckBoxView.h"
  21. static const CGFloat kHeight = 36.0f;
  22. @interface SSCheckBoxView(Private)
  23. - (UIImage *) checkBoxImageForStyle:(SSCheckBoxViewStyle)s
  24. checked:(BOOL)isChecked;
  25. - (CGRect) imageViewFrameForCheckBoxImage:(UIImage *)img;
  26. - (void) updateCheckBoxImage;
  27. @end
  28. @implementation SSCheckBoxView
  29. @synthesize style, checked, enabled;
  30. @synthesize stateChangedBlock;
  31. - (id) initWithFrame:(CGRect)frame
  32. style:(SSCheckBoxViewStyle)aStyle
  33. checked:(BOOL)aChecked
  34. {
  35. frame.size.height = kHeight;
  36. if (!(self = [super initWithFrame:frame])) {
  37. return self;
  38. }
  39. stateChangedSelector = nil;
  40. self.stateChangedBlock = nil;
  41. delegate = nil;
  42. style = aStyle;
  43. checked = aChecked;
  44. self.enabled = YES;
  45. self.userInteractionEnabled = YES;
  46. self.backgroundColor = [UIColor clearColor];
  47. CGRect labelFrame = CGRectMake(32.0f, 7.0f, self.frame.size.width - 32, 20.0f);
  48. UILabel *l = [[UILabel alloc] initWithFrame:labelFrame];
  49. l.textAlignment = NSTextAlignmentLeft;
  50. l.backgroundColor = [UIColor clearColor];
  51. l.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  52. l.textColor = [UIColor colorWithRed:((0x2E) / 256.0) green:((0x2E) / 256.0) blue:((0x2E) / 256.0) alpha:1.0];
  53. l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  54. l.shadowColor = [UIColor whiteColor];
  55. l.shadowOffset = CGSizeMake(0, 1);
  56. [self addSubview:l];
  57. textLabel = l;
  58. UIImage *img = [self checkBoxImageForStyle:style checked:checked];
  59. CGRect imageViewFrame = [self imageViewFrameForCheckBoxImage:img];
  60. UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
  61. iv.image = img;
  62. [self addSubview:iv];
  63. checkBoxImageView = iv;
  64. return self;
  65. }
  66. - (void) dealloc
  67. {
  68. self.stateChangedBlock = nil;
  69. //[checkBoxImageView release];
  70. //[textLabel release];
  71. //[super dealloc];
  72. }
  73. - (void) setEnabled:(BOOL)isEnabled
  74. {
  75. textLabel.enabled = isEnabled;
  76. enabled = isEnabled;
  77. checkBoxImageView.alpha = isEnabled ? 1.0f: 0.6f;
  78. }
  79. - (BOOL) enabled
  80. {
  81. return enabled;
  82. }
  83. - (void) setText:(NSString *)text
  84. {
  85. [textLabel setText:text];
  86. }
  87. - (void) setChecked:(BOOL)isChecked
  88. {
  89. checked = isChecked;
  90. [self updateCheckBoxImage];
  91. }
  92. - (void) setStateChangedTarget:(id<NSObject>)target
  93. selector:(SEL)selector
  94. {
  95. delegate = target;
  96. stateChangedSelector = selector;
  97. }
  98. #pragma mark -
  99. #pragma mark Touch-related Methods
  100. - (void) touchesBegan:(NSSet *)touches
  101. withEvent:(UIEvent *)event
  102. {
  103. if (!enabled) {
  104. return;
  105. }
  106. self.alpha = 0.8f;
  107. [super touchesBegan:touches withEvent:event];
  108. }
  109. - (void) touchesCancelled:(NSSet *)touches
  110. withEvent:(UIEvent *)event
  111. {
  112. if (!enabled) {
  113. return;
  114. }
  115. self.alpha = 1.0f;
  116. [super touchesCancelled:touches withEvent:event];
  117. }
  118. - (void) touchesEnded:(NSSet *)touches
  119. withEvent:(UIEvent *)event
  120. {
  121. if (!enabled) {
  122. return;
  123. }
  124. // restore alpha
  125. self.alpha = 1.0f;
  126. // check touch up inside
  127. if ([self superview]) {
  128. UITouch *touch = [touches anyObject];
  129. CGPoint point = [touch locationInView:[self superview]];
  130. CGRect validTouchArea = CGRectMake((self.frame.origin.x - 5),
  131. (self.frame.origin.y - 10),
  132. (self.frame.size.width + 5),
  133. (self.frame.size.height + 10));
  134. if (CGRectContainsPoint(validTouchArea, point)) {
  135. checked = !checked;
  136. [self updateCheckBoxImage];
  137. if (delegate && stateChangedSelector) {
  138. [delegate performSelector:stateChangedSelector withObject:self];
  139. }
  140. else if (stateChangedBlock) {
  141. stateChangedBlock(self);
  142. }
  143. }
  144. }
  145. [super touchesEnded:touches withEvent:event];
  146. }
  147. - (BOOL) canBecomeFirstResponder
  148. {
  149. return YES;
  150. }
  151. #pragma mark -
  152. #pragma mark Private Methods
  153. - (UIImage *) checkBoxImageForStyle:(SSCheckBoxViewStyle)s
  154. checked:(BOOL)isChecked
  155. {
  156. NSString *suffix = isChecked ? @"on" : @"off";
  157. NSString *imageName = @"";
  158. switch (s) {
  159. case kSSCheckBoxViewStyleBox:
  160. imageName = @"cb_box_";
  161. break;
  162. case kSSCheckBoxViewStyleDark:
  163. imageName = @"cb_dark_";
  164. break;
  165. case kSSCheckBoxViewStyleGlossy:
  166. imageName = @"cb_glossy_";
  167. break;
  168. case kSSCheckBoxViewStyleGreen:
  169. imageName = @"cb_green_";
  170. break;
  171. case kSSCheckBoxViewStyleMono:
  172. imageName = @"cb_mono_";
  173. break;
  174. case kSSCheckBoxViewStyleClick:
  175. imageName = @"cb_click_";
  176. break;
  177. case kSSCheckBoxViewStyleRound:
  178. imageName = @"cb_round_";
  179. break;
  180. default:
  181. return nil;
  182. }
  183. imageName = [NSString stringWithFormat:@"%@%@", imageName, suffix];
  184. return [UIImage imageNamed:imageName];
  185. }
  186. - (CGRect) imageViewFrameForCheckBoxImage:(UIImage *)img
  187. {
  188. CGFloat y = floorf((kHeight - img.size.height) / 2.0f);
  189. return CGRectMake(5.0f, y, img.size.width, img.size.height);
  190. }
  191. - (void) updateCheckBoxImage
  192. {
  193. checkBoxImageView.image = [self checkBoxImageForStyle:style
  194. checked:checked];
  195. }
  196. @end