|
| 1 | +// |
| 2 | +// SCLSwitchView.m |
| 3 | +// SCLAlertView |
| 4 | +// |
| 5 | +// Created by André Felipe Santos on 27/01/16. |
| 6 | +// Copyright © 2016 AnyKey Entertainment. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "SCLSwitchView.h" |
| 10 | +#import "SCLMacros.h" |
| 11 | + |
| 12 | +@interface SCLSwitchView () |
| 13 | + |
| 14 | +@property (strong, nonatomic) UISwitch *switchKnob; |
| 15 | +@property (strong, nonatomic) UILabel *switchLabel; |
| 16 | + |
| 17 | +@end |
| 18 | + |
| 19 | +#pragma mark |
| 20 | + |
| 21 | +@implementation SCLSwitchView |
| 22 | + |
| 23 | +#pragma mark - Constructors |
| 24 | + |
| 25 | +- (instancetype)init |
| 26 | +{ |
| 27 | + self = [super init]; |
| 28 | + if (self) |
| 29 | + { |
| 30 | + [self setup]; |
| 31 | + } |
| 32 | + return self; |
| 33 | +} |
| 34 | + |
| 35 | +- (instancetype)initWithCoder:(NSCoder *)aDecoder |
| 36 | +{ |
| 37 | + self = [super initWithCoder:aDecoder]; |
| 38 | + if(self) |
| 39 | + { |
| 40 | + [self setup]; |
| 41 | + } |
| 42 | + return self; |
| 43 | +} |
| 44 | + |
| 45 | +- (instancetype)initWithFrame:(CGRect)frame |
| 46 | +{ |
| 47 | + self = [super initWithFrame:frame]; |
| 48 | + if (self) |
| 49 | + { |
| 50 | + [self setup]; |
| 51 | + } |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +#pragma mark - Initialization |
| 56 | + |
| 57 | +- (void)setup |
| 58 | +{ |
| 59 | + // Add switch knob |
| 60 | + self.switchKnob = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.f)]; |
| 61 | + [self addSubview:self.switchKnob]; |
| 62 | + |
| 63 | + // Add switch label |
| 64 | + CGFloat x, width, height; |
| 65 | + x = self.switchKnob.frame.size.width + 8.0f; |
| 66 | + width = self.frame.size.width - self.switchKnob.frame.size.width - 8.0f; |
| 67 | + height = self.switchKnob.frame.size.height; |
| 68 | + |
| 69 | + self.switchLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 0.0f, width, height)]; |
| 70 | + |
| 71 | + NSString *switchFontFamily = @"HelveticaNeue-Bold"; |
| 72 | + CGFloat switchFontSize = 12.0f; |
| 73 | + |
| 74 | + self.switchLabel.numberOfLines = 1; |
| 75 | + self.switchLabel.textAlignment = NSTextAlignmentLeft; |
| 76 | + self.switchLabel.lineBreakMode = NSLineBreakByTruncatingTail; |
| 77 | + self.switchLabel.adjustsFontSizeToFitWidth = YES; |
| 78 | + self.switchLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters; |
| 79 | + self.switchLabel.minimumScaleFactor = 0.5f; |
| 80 | + self.switchLabel.font = [UIFont fontWithName:switchFontFamily size:switchFontSize]; |
| 81 | + self.switchLabel.textColor = UIColorFromHEX(0x4D4D4D); |
| 82 | + |
| 83 | + [self addSubview:self.switchLabel]; |
| 84 | +} |
| 85 | + |
| 86 | +#pragma mark - Getters |
| 87 | + |
| 88 | +- (UIColor *)tintColor { |
| 89 | + return self.switchKnob.tintColor; |
| 90 | +} |
| 91 | + |
| 92 | +- (UIColor *)labelColor { |
| 93 | + return self.switchLabel.textColor; |
| 94 | +} |
| 95 | + |
| 96 | +- (UIFont *)labelFont { |
| 97 | + return self.switchLabel.font; |
| 98 | +} |
| 99 | + |
| 100 | +- (NSString *)labelText { |
| 101 | + return self.switchLabel.text; |
| 102 | +} |
| 103 | + |
| 104 | +- (BOOL)isSelected { |
| 105 | + return self.switchKnob.isOn; |
| 106 | +} |
| 107 | + |
| 108 | +#pragma mark - Setters |
| 109 | + |
| 110 | +- (void)setTintColor:(UIColor *)tintColor { |
| 111 | + self.switchKnob.onTintColor = tintColor; |
| 112 | +} |
| 113 | + |
| 114 | +- (void)setLabelColor:(UIColor *)labelColor { |
| 115 | + self.switchLabel.textColor = labelColor; |
| 116 | +} |
| 117 | + |
| 118 | +- (void)setLabelFont:(UIFont *)labelFont { |
| 119 | + self.switchLabel.font = labelFont; |
| 120 | +} |
| 121 | + |
| 122 | +- (void)setLabelText:(NSString *)labelText { |
| 123 | + self.switchLabel.text = labelText; |
| 124 | +} |
| 125 | + |
| 126 | +@end |
0 commit comments