SCLButton.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // SCLButton.m
  3. // SCLAlertView
  4. //
  5. // Created by Diogo Autilio on 9/26/14.
  6. // Copyright (c) 2014-2017 AnyKey Entertainment. All rights reserved.
  7. //
  8. #import "SCLButton.h"
  9. #import "SCLTimerDisplay.h"
  10. #define MARGIN_BUTTON 12.0f
  11. #define DEFAULT_WINDOW_WIDTH 240
  12. #define MIN_HEIGHT 35.0f
  13. @implementation SCLButton
  14. - (instancetype)init
  15. {
  16. self = [super init];
  17. if (self)
  18. {
  19. [self setupWithWindowWidth:DEFAULT_WINDOW_WIDTH];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithWindowWidth:(CGFloat)windowWidth
  24. {
  25. self = [super init];
  26. if (self)
  27. {
  28. [self setupWithWindowWidth:windowWidth];
  29. }
  30. return self;
  31. }
  32. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  33. {
  34. self = [super initWithCoder:aDecoder];
  35. if(self)
  36. {
  37. [self setupWithWindowWidth:DEFAULT_WINDOW_WIDTH];
  38. }
  39. return self;
  40. }
  41. - (instancetype)initWithFrame:(CGRect)frame
  42. {
  43. self = [super initWithFrame:frame];
  44. if (self)
  45. {
  46. [self setupWithWindowWidth:DEFAULT_WINDOW_WIDTH];
  47. }
  48. return self;
  49. }
  50. - (void)setupWithWindowWidth:(CGFloat)windowWidth
  51. {
  52. self.frame = CGRectMake(0.0f, 0.0f, windowWidth - (MARGIN_BUTTON * 2), MIN_HEIGHT);
  53. self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
  54. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  55. self.layer.cornerRadius = 3.0f;
  56. }
  57. - (void)adjustWidthWithWindowWidth:(CGFloat)windowWidth numberOfButtons:(NSUInteger)numberOfButtons
  58. {
  59. CGFloat allButtonsWidth = windowWidth - (MARGIN_BUTTON * 2);
  60. CGFloat buttonWidth = (allButtonsWidth - ((numberOfButtons - 1) * 10)) / numberOfButtons;
  61. self.frame = CGRectMake(0.0f, 0.0f, buttonWidth, MIN_HEIGHT);
  62. }
  63. - (void)setTitle:(NSString *)title forState:(UIControlState)state
  64. {
  65. [super setTitle:title forState:state];
  66. self.titleLabel.numberOfLines = 0;
  67. // Update title frame.
  68. [self.titleLabel sizeToFit];
  69. // Update button frame
  70. [self layoutIfNeeded];
  71. // Get height needed to display title label completely
  72. CGFloat buttonHeight = MAX(self.titleLabel.frame.size.height, MIN_HEIGHT);
  73. // Update button frame
  74. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, buttonHeight);
  75. }
  76. - (void)setHighlighted:(BOOL)highlighted
  77. {
  78. self.backgroundColor = (highlighted) ? [self darkerColorForColor:_defaultBackgroundColor] : _defaultBackgroundColor;
  79. [super setHighlighted:highlighted];
  80. }
  81. - (void)setDefaultBackgroundColor:(UIColor *)defaultBackgroundColor
  82. {
  83. self.backgroundColor = _defaultBackgroundColor = defaultBackgroundColor;
  84. }
  85. - (void)setTimer:(SCLTimerDisplay *)timer
  86. {
  87. _timer = timer;
  88. [self addSubview:timer];
  89. [timer updateFrame:self.frame.size];
  90. timer.color = self.titleLabel.textColor;
  91. }
  92. #pragma mark - Button Apperance
  93. - (void)parseConfig:(NSDictionary *)buttonConfig
  94. {
  95. if (buttonConfig[@"backgroundColor"])
  96. {
  97. self.defaultBackgroundColor = buttonConfig[@"backgroundColor"];
  98. }
  99. if (buttonConfig[@"textColor"])
  100. {
  101. [self setTitleColor:buttonConfig[@"textColor"] forState:UIControlStateNormal];
  102. }
  103. if (buttonConfig[@"cornerRadius"])
  104. {
  105. self.layer.cornerRadius = [buttonConfig[@"cornerRadius"] floatValue];
  106. }
  107. if ((buttonConfig[@"borderColor"]) && (buttonConfig[@"borderWidth"]))
  108. {
  109. self.layer.borderColor = ((UIColor*)buttonConfig[@"borderColor"]).CGColor;
  110. self.layer.borderWidth = [buttonConfig[@"borderWidth"] floatValue];
  111. }
  112. else if (buttonConfig[@"borderWidth"])
  113. {
  114. self.layer.borderWidth = [buttonConfig[@"borderWidth"] floatValue];
  115. }
  116. // Add Button custom font with buttonConfig parameters
  117. if (buttonConfig[@"font"]) {
  118. self.titleLabel.font = buttonConfig[@"font"];
  119. }
  120. }
  121. #pragma mark - Helpers
  122. - (UIColor *)darkerColorForColor:(UIColor *)color
  123. {
  124. CGFloat r, g, b, a;
  125. if ([color getRed:&r green:&g blue:&b alpha:&a])
  126. return [UIColor colorWithRed:MAX(r - 0.2f, 0.0f)
  127. green:MAX(g - 0.2f, 0.0f)
  128. blue:MAX(b - 0.2f, 0.0f)
  129. alpha:a];
  130. return nil;
  131. }
  132. - (UIColor *)lighterColorForColor:(UIColor *)color
  133. {
  134. CGFloat r, g, b, a;
  135. if ([color getRed:&r green:&g blue:&b alpha:&a])
  136. return [UIColor colorWithRed:MIN(r + 0.2f, 1.0f)
  137. green:MIN(g + 0.2f, 1.0f)
  138. blue:MIN(b + 0.2f, 1.0f)
  139. alpha:a];
  140. return nil;
  141. }
  142. @end