酷店

UIButton+ImageTitleStyle.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // UIButton+ImageTitleStyle.m
  3. // ACGPro
  4. //
  5. // Created by kangzhiqiang on 16/5/4.
  6. // Copyright © 2016年 CCJ. All rights reserved.
  7. //
  8. #import "UIButton+ImageTitleStyle.h"
  9. @implementation UIButton (ImageTitleStyle)
  10. -(void)setButtonImageTitleStyle:(ButtonImageTitleStyle)style padding:(CGFloat)padding
  11. {
  12. if (self.imageView.image != nil && self.titleLabel.text != nil)
  13. {
  14. self.adjustsImageWhenHighlighted=NO;
  15. //先还原
  16. self.titleEdgeInsets = UIEdgeInsetsZero;
  17. self.imageEdgeInsets = UIEdgeInsetsZero;
  18. CGRect imageRect = self.imageView.frame;
  19. CGRect titleRect = self.titleLabel.frame;
  20. CGFloat totalHeight = imageRect.size.height + padding + titleRect.size.height;
  21. CGFloat selfHeight = self.frame.size.height;
  22. CGFloat selfWidth = self.frame.size.width;
  23. switch (style) {
  24. case ButtonImageTitleStyleLeft:
  25. if (padding != 0)
  26. {
  27. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  28. padding/2,
  29. 0,
  30. -padding/2);
  31. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  32. -padding/2,
  33. 0,
  34. padding/2);
  35. }
  36. break;
  37. case ButtonImageTitleStyleRight:
  38. {
  39. //图片在右,文字在左
  40. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  41. -(imageRect.size.width + padding/2),
  42. 0,
  43. (imageRect.size.width + padding/2));
  44. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  45. (titleRect.size.width+ padding/2),
  46. 0,
  47. -(titleRect.size.width+ padding/2));
  48. }
  49. break;
  50. case ButtonImageTitleStyleTop:
  51. {
  52. //图片在上,文字在下
  53. self.titleEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 + imageRect.size.height + padding - titleRect.origin.y),
  54. (selfWidth/2 - titleRect.origin.x - titleRect.size.width /2) - (selfWidth - titleRect.size.width) / 2,
  55. -((selfHeight - totalHeight)/2 + imageRect.size.height + padding - titleRect.origin.y),
  56. -(selfWidth/2 - titleRect.origin.x - titleRect.size.width /2) - (selfWidth - titleRect.size.width) / 2);
  57. self.imageEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 - imageRect.origin.y),
  58. (selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2),
  59. -((selfHeight - totalHeight)/2 - imageRect.origin.y),
  60. -(selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2));
  61. }
  62. break;
  63. case ButtonImageTitleStyleBottom:
  64. {
  65. //图片在下,文字在上。
  66. self.titleEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 - titleRect.origin.y),
  67. (selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  68. -((selfHeight - totalHeight)/2 - titleRect.origin.y),
  69. -(selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  70. self.imageEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 + titleRect.size.height + padding - imageRect.origin.y),
  71. (selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2),
  72. -((selfHeight - totalHeight)/2 + titleRect.size.height + padding - imageRect.origin.y),
  73. -(selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2));
  74. }
  75. break;
  76. case ButtonImageTitleStyleCenterTop:
  77. {
  78. self.titleEdgeInsets = UIEdgeInsetsMake(-(titleRect.origin.y - padding),
  79. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  80. (titleRect.origin.y - padding),
  81. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  82. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  83. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  84. 0,
  85. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  86. }
  87. break;
  88. case ButtonImageTitleStyleCenterBottom:
  89. {
  90. self.titleEdgeInsets = UIEdgeInsetsMake((selfHeight - padding - titleRect.origin.y - titleRect.size.height),
  91. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  92. -(selfHeight - padding - titleRect.origin.y - titleRect.size.height),
  93. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  94. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  95. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  96. 0,
  97. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  98. }
  99. break;
  100. case ButtonImageTitleStyleCenterUp:
  101. {
  102. self.titleEdgeInsets = UIEdgeInsetsMake(-(titleRect.origin.y + titleRect.size.height - imageRect.origin.y + padding),
  103. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  104. (titleRect.origin.y + titleRect.size.height - imageRect.origin.y + padding),
  105. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  106. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  107. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  108. 0,
  109. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  110. }
  111. break;
  112. case ButtonImageTitleStyleCenterDown:
  113. {
  114. self.titleEdgeInsets = UIEdgeInsetsMake((imageRect.origin.y + imageRect.size.height - titleRect.origin.y + padding),
  115. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  116. -(imageRect.origin.y + imageRect.size.height - titleRect.origin.y + padding),
  117. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  118. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  119. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  120. 0,
  121. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  122. }
  123. break;
  124. case ButtonImageTitleStyleRightLeft:
  125. {
  126. //图片在右,文字在左,距离按钮两边边距
  127. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  128. -(titleRect.origin.x - padding),
  129. 0,
  130. (titleRect.origin.x - padding));
  131. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  132. (selfWidth - padding - imageRect.origin.x - imageRect.size.width),
  133. 0,
  134. -(selfWidth - padding - imageRect.origin.x - imageRect.size.width));
  135. }
  136. break;
  137. case ButtonImageTitleStyleLeftRight:
  138. {
  139. //图片在左,文字在右,距离按钮两边边距
  140. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  141. (selfWidth - padding - titleRect.origin.x - titleRect.size.width),
  142. 0,
  143. -(selfWidth - padding - titleRect.origin.x - titleRect.size.width));
  144. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  145. -(imageRect.origin.x - padding),
  146. 0,
  147. (imageRect.origin.x - padding));
  148. }
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. else {
  155. self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  156. self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  157. }
  158. }
  159. - (void)setHighlighted:(BOOL)highlighted{
  160. }
  161. @end