SCLSwitchView.m 2.8 KB

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