CustomButton.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // CustomButton.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 2013. 12. 16..
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "UIView+AutoLayoutDebugging.h"
  9. #import "UIImage+Addtions.h"
  10. #import "CommonUtil.h"
  11. #import "CustomButton.h"
  12. @interface CustomButton () {
  13. UIImage *_imgNormal, *_imgHighlighted;
  14. UIImage *_bgImageNormal, *_bgImageHighlited;
  15. UIEdgeInsets _insets, _bgInsets;
  16. }
  17. @end
  18. @implementation CustomButton
  19. #define kfRoundWidth 4.0f
  20. - (void)setImageStratchForState:(UIControlState)state capInsets:(UIEdgeInsets)insets {
  21. UIImage *image = [self imageForState:state];
  22. image = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:image];
  23. [super setImage:image forState:state];
  24. }
  25. - (void)setBackgroundImageStretchForState:(UIControlState)state capInsets:(UIEdgeInsets)insets {
  26. UIImage *bimage = [self backgroundImageForState:state];
  27. bimage = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:bimage];
  28. [super setBackgroundImage:bimage forState:state];
  29. }
  30. - (instancetype)initWithFrame:(CGRect)frame {
  31. if (self = [super initWithFrame:frame]) {
  32. if (!CGRectEqualToRect(_rectForCap, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
  33. CGFloat top, left, bottom, right;
  34. top = CGRectGetMinY(_rectForCap);
  35. left = CGRectGetMinX(_rectForCap);
  36. bottom = CGRectGetHeight(_rectForCap);
  37. right = CGRectGetWidth(_rectForCap);
  38. _insets = UIEdgeInsetsMake(top, left, bottom, right);
  39. [self setImageStratchForState:UIControlStateNormal capInsets:_insets];
  40. [self setImageStratchForState:UIControlStateHighlighted capInsets:_insets];
  41. }
  42. if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
  43. CGFloat top, left, bottom, right;
  44. top = CGRectGetMinY(_rectForCapBackground);
  45. left = CGRectGetMinX(_rectForCapBackground);
  46. bottom = CGRectGetHeight(_rectForCapBackground);
  47. right = CGRectGetWidth(_rectForCapBackground);
  48. _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);
  49. [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
  50. [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
  51. [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
  52. } else {//rectForCap이 설정되지 않은 경우, 픽스된 사이즈로 이미지를 리사이즈함.
  53. _bgInsets = UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth);
  54. [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
  55. [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
  56. [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
  57. }
  58. }
  59. return self;
  60. }
  61. - (void)awakeFromNib {//NIB로 로드될 경우,
  62. if (!CGRectEqualToRect(_rectForCap, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
  63. CGFloat top, left, bottom, right;
  64. top = CGRectGetMinY(_rectForCap);
  65. left = CGRectGetMinX(_rectForCap);
  66. bottom = CGRectGetHeight(_rectForCap);
  67. right = CGRectGetWidth(_rectForCap);
  68. _insets = UIEdgeInsetsMake(top, left, bottom, right);
  69. [self setImageStratchForState:UIControlStateNormal capInsets:_insets];
  70. [self setImageStratchForState:UIControlStateHighlighted capInsets:_insets];
  71. }
  72. if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
  73. if (!CGRectEqualToRect(_rectForCapBackground, CGRectMake(-1, 0, 0, 0))) {
  74. CGFloat top, left, bottom, right;
  75. top = CGRectGetMinY(_rectForCapBackground);
  76. left = CGRectGetMinX(_rectForCapBackground);
  77. bottom = CGRectGetHeight(_rectForCapBackground);
  78. right = CGRectGetWidth(_rectForCapBackground);
  79. _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);
  80. [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
  81. [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
  82. [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
  83. }
  84. } else {//rectForCap이 설정되지 않은 경우, 픽스된 사이즈로 이미지를 리사이즈함.
  85. _bgInsets = UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth);
  86. [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
  87. [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
  88. [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
  89. }
  90. _textColorNormal = [self titleColorForState:UIControlStateNormal];
  91. if (_placeHolder) {
  92. [self setTitle:_placeHolder forState:UIControlStateNormal];
  93. }
  94. if (_placeHolderColor) {
  95. [self setTitleColor:_placeHolderColor forState:UIControlStateNormal];
  96. }
  97. #ifdef DEBUG
  98. NSString *identifier = [NSString stringWithFormat:@"%@", [self titleForState:UIControlStateNormal]];
  99. if ([identifier isEqualToString:@"(null)"] || !identifier || [identifier isEqualToString:ksEmptyString]) {
  100. identifier = NSStringFromCGRect(self.frame);
  101. }
  102. [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
  103. #endif
  104. }
  105. #pragma mark - Properties
  106. - (void)setPlaceHolder:(NSString *)placeHolder {
  107. _placeHolder = placeHolder;
  108. [self setTitle:_placeHolder forState:UIControlStateNormal];
  109. }
  110. - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state {//백그라운드 이미지를 버튼 크기에 맞게 조정함.
  111. image = [CommonUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:image];
  112. [super setBackgroundImage:image forState:state];
  113. }
  114. - (void)setFrame:(CGRect)frame {
  115. [super setFrame:frame];
  116. if ([self backgroundImageForState:UIControlStateNormal]) {//백그라운드 이미지를 버튼 크기에 맞게 조정함.
  117. [self setBackgroundImage:[CommonUtil stretchedImage:[self backgroundImageForState:UIControlStateNormal] expectSize:self.frame.size] forState:UIControlStateNormal];
  118. }
  119. if ([self backgroundImageForState:UIControlStateHighlighted]) {//백그라운드 이미지를 버튼 크기에 맞게 조정함.
  120. [self setBackgroundImage:[CommonUtil stretchedImage:[self backgroundImageForState:UIControlStateHighlighted] expectSize:self.frame.size] forState:UIControlStateHighlighted];
  121. }
  122. }
  123. //- (void)setUnderLine:(BOOL)setUnderLine {
  124. //
  125. // NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[self titleForState:UIControlStateNormal]];
  126. // [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [attrString length])];
  127. //
  128. // UIColor* textColor = [self titleColorForState:UIControlStateNormal];
  129. //
  130. // NSNumber *underLine = setUnderLine ? [NSNumber numberWithInteger:NSUnderlineStyleSingle] : [NSNumber numberWithInteger:NSUnderlineStyleNone];
  131. //
  132. // [attrString setAttributes:@{NSForegroundColorAttributeName:textColor,NSUnderlineStyleAttributeName:underLine} range:NSMakeRange(0,[attrString length])];
  133. //
  134. // [self setAttributedTitle:attrString forState:UIControlStateNormal];
  135. //}
  136. //- (void)setTitle:(NSString *)title forState:(UIControlState)state {
  137. // [super setTitle:title forState:UIControlStateNormal];
  138. //
  139. // NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[self titleForState:state]];
  140. // UIColor* textColor = _textColorNormal ? _textColorNormal : [self titleColorForState:state];
  141. // [attrString setAttributes:@{NSForegroundColorAttributeName:textColor} range:NSMakeRange(0, [attrString length])];
  142. //
  143. // [self setAttributedTitle:attrString forState:state];
  144. //}
  145. //
  146. //- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state {
  147. // [super setTitleColor:color forState:state];
  148. //
  149. // NSMutableAttributedString *attrString = nil;
  150. // if (![self attributedTitleForState:state]) {
  151. // attrString = [[NSMutableAttributedString alloc] initWithString:[self titleForState:state]];
  152. // [attrString addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, [self titleForState:state].length)];
  153. // } else {
  154. // attrString = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitleForState:state]];
  155. // [attrString setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, [self titleForState:state].length)];
  156. // }
  157. //
  158. // [self setAttributedTitle:attrString forState:state];
  159. //}
  160. - (void)faceOffImage {
  161. if (!_imgNormal && !_imgHighlighted) {
  162. _imgNormal = [self imageForState:UIControlStateNormal];
  163. _imgHighlighted = [self imageForState:UIControlStateHighlighted];
  164. }
  165. if (_imgNormal && _imgHighlighted) {
  166. if ([_imgNormal isEqual:[self imageForState:UIControlStateNormal]]) {
  167. [self setImage:_imgHighlighted forState:UIControlStateNormal];
  168. [self setImage:_imgNormal forState:UIControlStateHighlighted];
  169. } else {
  170. [self setImage:_imgNormal forState:UIControlStateNormal];
  171. [self setImage:_imgHighlighted forState:UIControlStateHighlighted];
  172. }
  173. }
  174. //FIXME : weird
  175. // if (!_bgImageNormal && !_bgImageHighlited) {
  176. _bgImageNormal = [self backgroundImageForState:UIControlStateNormal];
  177. _bgImageHighlited = [self backgroundImageForState:UIControlStateHighlighted];
  178. // }
  179. if (_bgImageNormal && _bgImageHighlited) {
  180. if ([_bgImageNormal isEqual:[self backgroundImageForState:UIControlStateNormal]]) {
  181. [self setBackgroundImage:_bgImageHighlited forState:UIControlStateNormal];
  182. [self setBackgroundImage:_bgImageNormal forState:UIControlStateHighlighted];
  183. } else {
  184. [self setBackgroundImage:_bgImageNormal forState:UIControlStateNormal];
  185. [self setBackgroundImage:_bgImageHighlited forState:UIControlStateHighlighted];
  186. }
  187. [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
  188. [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
  189. }
  190. }
  191. @end
  192. @implementation CustomLabelButton
  193. @end