CustomButton.m 12 KB

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