diff --git a/CTCheckbox/CTCheckbox.m b/CTCheckbox/CTCheckbox.m index d4cce37..74fa512 100644 --- a/CTCheckbox/CTCheckbox.m +++ b/CTCheckbox/CTCheckbox.m @@ -8,6 +8,8 @@ #import "CTCheckbox.h" +static const float CTCheckboxDefaultSideLength = 20.0; + @implementation CTCheckbox - (id)initWithFrame:(CGRect)frame @@ -30,10 +32,11 @@ - (id)initWithCoder:(NSCoder *)aDecoder - (void)setUp { - self.checkboxSideLength = 20.0; + self.checkboxSideLength = CTCheckboxDefaultSideLength; self.checkboxColor = [UIColor blackColor]; self.backgroundColor = [UIColor clearColor]; self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero]; + self.textLabel.backgroundColor = [UIColor clearColor]; [self addSubview:self.textLabel]; } @@ -49,41 +52,27 @@ - (void)setChecked:(BOOL)checked - (void)drawRect:(CGRect)rect { CGRect frame = CGRectIntegral(CGRectMake(0, (rect.size.height - self.checkboxSideLength) / 2.0, self.checkboxSideLength, self.checkboxSideLength)); - UIBezierPath *bezierPath = [UIBezierPath bezierPath]; if (self.checked) { - [bezierPath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 0.75000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.21875 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.40000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.52500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.28125 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.17500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.47500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.40000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.75000 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.81250 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.28125 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.75000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.21875 * CGRectGetHeight(frame))]; + UIBezierPath *bezierPath = [UIBezierPath bezierPath]; + + [bezierPath moveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.75000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.21875 * CGRectGetHeight(frame))]; + [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.40000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.52500 * CGRectGetHeight(frame))]; + [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.28125 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37500 * CGRectGetHeight(frame))]; + [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.17500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.47500 * CGRectGetHeight(frame))]; + [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.40000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.75000 * CGRectGetHeight(frame))]; + [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.81250 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.28125 * CGRectGetHeight(frame))]; + [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.75000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.21875 * CGRectGetHeight(frame))]; [bezierPath closePath]; + + [self.checkboxColor setFill]; + [bezierPath fill]; } - [bezierPath moveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.87500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.12500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.87500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.87500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.12500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.87500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.12500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.12500 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.87500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.12500 * CGRectGetHeight(frame))]; - [bezierPath closePath]; - [bezierPath moveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.90625 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.00000 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.09375 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.00000 * CGRectGetHeight(frame))]; - [bezierPath addCurveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.09375 * CGRectGetHeight(frame)) controlPoint1:CGPointMake(CGRectGetMinX(frame) + 0.04219 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.00000 * CGRectGetHeight(frame)) controlPoint2:CGPointMake(CGRectGetMinX(frame) + 0.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.04219 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.90625 * CGRectGetHeight(frame))]; - [bezierPath addCurveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.09375 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 1.00000 * CGRectGetHeight(frame)) controlPoint1:CGPointMake(CGRectGetMinX(frame) + 0.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.95781 * CGRectGetHeight(frame)) controlPoint2:CGPointMake(CGRectGetMinX(frame) + 0.04219 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 1.00000 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.90625 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 1.00000 * CGRectGetHeight(frame))]; - [bezierPath addCurveToPoint:CGPointMake(CGRectGetMinX(frame) + 1.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.90625 * CGRectGetHeight(frame)) controlPoint1:CGPointMake(CGRectGetMinX(frame) + 0.95781 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 1.00000 * CGRectGetHeight(frame)) controlPoint2:CGPointMake(CGRectGetMinX(frame) + 1.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.95781 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 1.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.09375 * CGRectGetHeight(frame))]; - [bezierPath addCurveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.90625 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.00000 * CGRectGetHeight(frame)) controlPoint1:CGPointMake(CGRectGetMinX(frame) + 1.00000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.04219 * CGRectGetHeight(frame)) controlPoint2:CGPointMake(CGRectGetMinX(frame) + 0.95781 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.00000 * CGRectGetHeight(frame))]; - [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.90625 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.00000 * CGRectGetHeight(frame))]; - [bezierPath closePath]; - bezierPath.miterLimit = 4; - - - [self.checkboxColor setFill]; - [bezierPath fill]; + UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(CGRectGetMinX(frame) + floor(CGRectGetWidth(frame) * 0.05000 + 0.5), CGRectGetMinY(frame) + floor(CGRectGetHeight(frame) * 0.05000 + 0.5), floor(CGRectGetWidth(frame) * 0.95000 + 0.5) - floor(CGRectGetWidth(frame) * 0.05000 + 0.5), floor(CGRectGetHeight(frame) * 0.95000 + 0.5) - floor(CGRectGetHeight(frame) * 0.05000 + 0.5)) cornerRadius:4]; + roundedRectanglePath.lineWidth = 2 * self.checkboxSideLength / CTCheckboxDefaultSideLength; + [self.checkboxColor setStroke]; + [roundedRectanglePath stroke]; } - (void)layoutSubviews diff --git a/CTCheckboxSample/CTViewController.h b/CTCheckboxSample/CTViewController.h index e022d2e..8766afd 100644 --- a/CTCheckboxSample/CTViewController.h +++ b/CTCheckboxSample/CTViewController.h @@ -10,9 +10,10 @@ @class CTCheckbox; -@interface CTViewController : UIViewController +@interface CTViewController : UIViewController @property (weak, nonatomic) IBOutlet CTCheckbox *checkbox; +@property (weak, nonatomic) IBOutlet UITextField *textField; @property (weak, nonatomic) IBOutlet UILabel *label; - (IBAction)blackButtonTapped:(id)sender; diff --git a/CTCheckboxSample/CTViewController.m b/CTCheckboxSample/CTViewController.m index f3a7d97..d050163 100644 --- a/CTCheckboxSample/CTViewController.m +++ b/CTCheckboxSample/CTViewController.m @@ -12,6 +12,7 @@ @interface CTViewController () @property (nonatomic, strong) CTCheckbox *checkbox2; +@property (nonatomic, strong) CTCheckbox *checkbox3; @end @implementation CTViewController @@ -25,11 +26,17 @@ - (void)viewDidLoad [self checkboxDidChanged:self.checkbox]; self.checkbox2 = [[CTCheckbox alloc] initWithFrame:CGRectMake(20.0, 200.0, 280.0, 20.0)]; - self.checkbox2.textLabel.text= @"by initWithFrame:"; + self.checkbox2.textLabel.text = @"by initWithFrame:"; [self.view addSubview:self.checkbox2]; + + self.checkbox3 = [[CTCheckbox alloc] initWithFrame:CGRectMake(20.0, 240.0, 280.0, 40.0)]; + self.checkbox3.checkboxSideLength = 40.0; + self.checkbox3.textLabel.text = @"checkboxSideLength is 40.0"; + [self.view addSubview:self.checkbox3]; } -- (void)checkboxDidChanged:(CTCheckbox *)checkbox { +- (void)checkboxDidChanged:(CTCheckbox *)checkbox +{ if (checkbox.checked) { self.label.text = @"Checked"; } else { @@ -43,23 +50,35 @@ - (void)didReceiveMemoryWarning // Dispose of any resources that can be recreated. } -- (IBAction)blackButtonTapped:(id)sender { - self.checkbox.checkboxColor=[UIColor blackColor]; +- (IBAction)blackButtonTapped:(id)sender +{ + self.checkbox.checkboxColor = [UIColor blackColor]; [self.checkbox setNeedsDisplay]; } -- (IBAction)blueButtonTapped:(id)sender { - self.checkbox.checkboxColor=[UIColor blueColor]; +- (IBAction)blueButtonTapped:(id)sender +{ + self.checkbox.checkboxColor = [UIColor blueColor]; [self.checkbox setNeedsDisplay]; } -- (IBAction)redButtonTapped:(id)sender { - self.checkbox.checkboxColor=[UIColor redColor]; +- (IBAction)redButtonTapped:(id)sender +{ + self.checkbox.checkboxColor = [UIColor redColor]; [self.checkbox setNeedsDisplay]; } -- (IBAction)textChanged:(id)sender { - self.checkbox.textLabel.text= [(UITextField *)sender text]; +- (IBAction)textChanged:(id)sender +{ + self.checkbox.textLabel.text = [(UITextField *)sender text]; +} + +#pragma mark - UITextField delegate + +- (BOOL)textFieldShouldReturn:(UITextField *)textField +{ + [textField resignFirstResponder]; + return YES; } @end diff --git a/CTCheckboxSample/en.lproj/MainStoryboard.storyboard b/CTCheckboxSample/en.lproj/MainStoryboard.storyboard index eda435c..f54ce32 100644 --- a/CTCheckboxSample/en.lproj/MainStoryboard.storyboard +++ b/CTCheckboxSample/en.lproj/MainStoryboard.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -9,73 +10,59 @@ - + - + - + + @@ -84,28 +71,13 @@ + - - - - - - - - - - - - - - - -