JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
//
//  DCRoundSwitchToggleLayer.m
//
//  Created by Patrick Richards on 29/06/11.
//  MIT License.
//
//  http://twitter.com/patr
//  http://domesticcat.com.au/projects
//  http://github.com/domesticcatsoftware/DCRoundSwitch
//
 
#import "DCRoundSwitchToggleLayer.h"
 
@implementation DCRoundSwitchToggleLayer
@synthesize onString, offString, onTintColor;
@synthesize drawOnTint;
@synthesize clip;
@synthesize labelFont;
 
 
- (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString onTintColor:(UIColor *)anOnTintColor
{
    if ((self = [super init]))
    {
        self.onString = anOnString;
        self.offString = anOffString;
        self.onTintColor = anOnTintColor;
    }
 
    return self;
}
 
- (UIFont *)labelFont
{
    return [UIFont boldSystemFontOfSize:ceilf(self.bounds.size.height * .6)];
}
 
- (void)drawInContext:(CGContextRef)context
{
    CGFloat knobRadius = self.bounds.size.height - 2.0;
    CGFloat knobCenter = self.bounds.size.width / 2.0;
    CGRect knobRect = CGRectMake(knobCenter - knobRadius / 2.0, 1.0, knobRadius, knobRadius);
 
    if (self.clip)
    {
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-self.frame.origin.x + 0.5, 0, self.bounds.size.width / 2.0 + self.bounds.size.height / 2.0 - 1.5, self.bounds.size.height) cornerRadius:self.bounds.size.height / 2.0];
        CGContextAddPath(context, bezierPath.CGPath);
        CGContextClip(context);
    }
 
    // on tint color
    if (self.drawOnTint)
    {
        CGContextSetFillColorWithColor(context, self.onTintColor.CGColor);
        CGContextFillRect(context, CGRectMake(0, 0, knobCenter, self.bounds.size.height));
    }
 
    // off tint color (white)
    CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.963 alpha:1.0].CGColor);
    CGContextFillRect(context, CGRectMake(knobCenter, 0, self.bounds.size.width - knobCenter, self.bounds.size.height));
 
    // knob shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 1.5, [UIColor colorWithWhite:0.2 alpha:1.0].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.42 alpha:1.0].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextStrokeEllipseInRect(context, knobRect);
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 0, NULL);
    
 
    // strings
    CGFloat textSpaceWidth = (self.bounds.size.width / 2) - (knobRadius / 2);
 
    UIGraphicsPushContext(context);
 
    // 'ON' state label (self.onString)
    CGSize onTextSize = [self.onString sizeWithFont:self.labelFont];
    CGPoint onTextPoint = CGPointMake((textSpaceWidth - onTextSize.width) / 2.0 + knobRadius * .15, floorf((self.bounds.size.height - onTextSize.height) / 2.0) + 1.0);
    [[UIColor colorWithWhite:0.45 alpha:1.0] set]; // .2 & .4
    [self.onString drawAtPoint:CGPointMake(onTextPoint.x, onTextPoint.y - 1.0) withFont:self.labelFont];
    [[UIColor whiteColor] set];
    [self.onString drawAtPoint:onTextPoint withFont:self.labelFont];
 
    // 'OFF' state label (self.offString)
    CGSize offTextSize = [self.offString sizeWithFont:self.labelFont];
    CGPoint offTextPoint = CGPointMake(textSpaceWidth + (textSpaceWidth - offTextSize.width) / 2.0 + knobRadius * .86, floorf((self.bounds.size.height - offTextSize.height) / 2.0) + 1.0);
    [[UIColor whiteColor] set];
    [self.offString drawAtPoint:CGPointMake(offTextPoint.x, offTextPoint.y + 1.0) withFont:self.labelFont];
    [[UIColor colorWithWhite:0.52 alpha:1.0] set];
    [self.offString drawAtPoint:offTextPoint withFont:self.labelFont];
 
    UIGraphicsPopContext();
}
 
@end