| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- /*
- Copyright 2011 Ahmet Ardal
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- //
- // SSCheckBoxView.m
- // SSCheckBoxView
- //
- // Created by Ahmet Ardal on 12/6/11.
- // Copyright 2011 SpinningSphere Labs. All rights reserved.
- //
- #import "SSCheckBoxView.h"
- static const CGFloat kHeight = 36.0f;
- @interface SSCheckBoxView(Private)
- - (UIImage *) checkBoxImageForStyle:(SSCheckBoxViewStyle)s
- checked:(BOOL)isChecked;
- - (CGRect) imageViewFrameForCheckBoxImage:(UIImage *)img;
- - (void) updateCheckBoxImage;
- @end
- @implementation SSCheckBoxView
- @synthesize style, checked, enabled;
- @synthesize stateChangedBlock;
- - (id) initWithFrame:(CGRect)frame
- style:(SSCheckBoxViewStyle)aStyle
- checked:(BOOL)aChecked
- {
- frame.size.height = kHeight;
- if (!(self = [super initWithFrame:frame])) {
- return self;
- }
-
- stateChangedSelector = nil;
- self.stateChangedBlock = nil;
- delegate = nil;
- style = aStyle;
- checked = aChecked;
- self.enabled = YES;
-
- self.userInteractionEnabled = YES;
- self.backgroundColor = [UIColor clearColor];
-
- CGRect labelFrame = CGRectMake(32.0f, 7.0f, self.frame.size.width - 32, 20.0f);
- UILabel *l = [[UILabel alloc] initWithFrame:labelFrame];
- l.textAlignment = NSTextAlignmentLeft;
- l.backgroundColor = [UIColor clearColor];
- l.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
- l.textColor = [UIColor colorWithRed:((0x2E) / 256.0) green:((0x2E) / 256.0) blue:((0x2E) / 256.0) alpha:1.0];
-
- l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- l.shadowColor = [UIColor whiteColor];
- l.shadowOffset = CGSizeMake(0, 1);
- [self addSubview:l];
- textLabel = l;
-
- UIImage *img = [self checkBoxImageForStyle:style checked:checked];
- CGRect imageViewFrame = [self imageViewFrameForCheckBoxImage:img];
- UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
-
- iv.image = img;
- [self addSubview:iv];
- checkBoxImageView = iv;
-
- return self;
- }
- - (void) dealloc
- {
- self.stateChangedBlock = nil;
- //[checkBoxImageView release];
- //[textLabel release];
- //[super dealloc];
- }
- - (void) setEnabled:(BOOL)isEnabled
- {
- textLabel.enabled = isEnabled;
- enabled = isEnabled;
- checkBoxImageView.alpha = isEnabled ? 1.0f: 0.6f;
- }
- - (BOOL) enabled
- {
- return enabled;
- }
- - (void) setText:(NSString *)text
- {
- [textLabel setText:text];
- }
- - (void) setChecked:(BOOL)isChecked
- {
- checked = isChecked;
- [self updateCheckBoxImage];
- }
- - (void) setStateChangedTarget:(id<NSObject>)target
- selector:(SEL)selector
- {
- delegate = target;
- stateChangedSelector = selector;
- }
- #pragma mark -
- #pragma mark Touch-related Methods
- - (void) touchesBegan:(NSSet *)touches
- withEvent:(UIEvent *)event
- {
- if (!enabled) {
- return;
- }
-
- self.alpha = 0.8f;
- [super touchesBegan:touches withEvent:event];
- }
- - (void) touchesCancelled:(NSSet *)touches
- withEvent:(UIEvent *)event
- {
- if (!enabled) {
- return;
- }
-
- self.alpha = 1.0f;
- [super touchesCancelled:touches withEvent:event];
- }
- - (void) touchesEnded:(NSSet *)touches
- withEvent:(UIEvent *)event
- {
- if (!enabled) {
- return;
- }
-
- // restore alpha
- self.alpha = 1.0f;
-
- // check touch up inside
- if ([self superview]) {
- UITouch *touch = [touches anyObject];
- CGPoint point = [touch locationInView:[self superview]];
- CGRect validTouchArea = CGRectMake((self.frame.origin.x - 5),
- (self.frame.origin.y - 10),
- (self.frame.size.width + 5),
- (self.frame.size.height + 10));
- if (CGRectContainsPoint(validTouchArea, point)) {
- checked = !checked;
- [self updateCheckBoxImage];
- if (delegate && stateChangedSelector) {
- [delegate performSelector:stateChangedSelector withObject:self];
- }
- else if (stateChangedBlock) {
- stateChangedBlock(self);
- }
- }
- }
-
- [super touchesEnded:touches withEvent:event];
- }
- - (BOOL) canBecomeFirstResponder
- {
- return YES;
- }
- #pragma mark -
- #pragma mark Private Methods
- - (UIImage *) checkBoxImageForStyle:(SSCheckBoxViewStyle)s
- checked:(BOOL)isChecked
- {
- NSString *suffix = isChecked ? @"on" : @"off";
- NSString *imageName = @"";
- switch (s) {
- case kSSCheckBoxViewStyleBox:
- imageName = @"cb_box_";
- break;
- case kSSCheckBoxViewStyleDark:
- imageName = @"cb_dark_";
- break;
- case kSSCheckBoxViewStyleGlossy:
- imageName = @"cb_glossy_";
- break;
- case kSSCheckBoxViewStyleGreen:
- imageName = @"cb_green_";
- break;
- case kSSCheckBoxViewStyleMono:
- imageName = @"cb_mono_";
- break;
- case kSSCheckBoxViewStyleClick:
- imageName = @"cb_click_";
- break;
- case kSSCheckBoxViewStyleRound:
- imageName = @"cb_round_";
- break;
-
- default:
- return nil;
- }
- imageName = [NSString stringWithFormat:@"%@%@", imageName, suffix];
- return [UIImage imageNamed:imageName];
- }
- - (CGRect) imageViewFrameForCheckBoxImage:(UIImage *)img
- {
- CGFloat y = floorf((kHeight - img.size.height) / 2.0f);
- return CGRectMake(5.0f, y, img.size.width, img.size.height);
- }
- - (void) updateCheckBoxImage
- {
- checkBoxImageView.image = [self checkBoxImageForStyle:style
- checked:checked];
- }
- @end
|