CustomButton.m 11 KB

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