酷店

UIButton+ImageTitleStyle.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. //先还原
  15. self.titleEdgeInsets = UIEdgeInsetsZero;
  16. self.imageEdgeInsets = UIEdgeInsetsZero;
  17. CGRect imageRect = self.imageView.frame;
  18. CGRect titleRect = self.titleLabel.frame;
  19. CGFloat totalHeight = imageRect.size.height + padding + titleRect.size.height;
  20. CGFloat selfHeight = self.frame.size.height;
  21. CGFloat selfWidth = self.frame.size.width;
  22. switch (style) {
  23. case ButtonImageTitleStyleLeft:
  24. if (padding != 0)
  25. {
  26. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  27. padding/2,
  28. 0,
  29. -padding/2);
  30. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  31. -padding/2,
  32. 0,
  33. padding/2);
  34. }
  35. break;
  36. case ButtonImageTitleStyleRight:
  37. {
  38. //图片在右,文字在左
  39. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  40. -(imageRect.size.width + padding/2),
  41. 0,
  42. (imageRect.size.width + padding/2));
  43. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  44. (titleRect.size.width+ padding/2),
  45. 0,
  46. -(titleRect.size.width+ padding/2));
  47. }
  48. break;
  49. case ButtonImageTitleStyleTop:
  50. {
  51. //图片在上,文字在下
  52. self.titleEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 + imageRect.size.height + padding - titleRect.origin.y),
  53. (selfWidth/2 - titleRect.origin.x - titleRect.size.width /2) - (selfWidth - titleRect.size.width) / 2,
  54. -((selfHeight - totalHeight)/2 + imageRect.size.height + padding - titleRect.origin.y),
  55. -(selfWidth/2 - titleRect.origin.x - titleRect.size.width /2) - (selfWidth - titleRect.size.width) / 2);
  56. self.imageEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 - imageRect.origin.y),
  57. (selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2),
  58. -((selfHeight - totalHeight)/2 - imageRect.origin.y),
  59. -(selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2));
  60. }
  61. break;
  62. case ButtonImageTitleStyleBottom:
  63. {
  64. //图片在下,文字在上。
  65. self.titleEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 - titleRect.origin.y),
  66. (selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  67. -((selfHeight - totalHeight)/2 - titleRect.origin.y),
  68. -(selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  69. self.imageEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 + titleRect.size.height + padding - imageRect.origin.y),
  70. (selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2),
  71. -((selfHeight - totalHeight)/2 + titleRect.size.height + padding - imageRect.origin.y),
  72. -(selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2));
  73. }
  74. break;
  75. case ButtonImageTitleStyleCenterTop:
  76. {
  77. self.titleEdgeInsets = UIEdgeInsetsMake(-(titleRect.origin.y - padding),
  78. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  79. (titleRect.origin.y - padding),
  80. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  81. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  82. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  83. 0,
  84. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  85. }
  86. break;
  87. case ButtonImageTitleStyleCenterBottom:
  88. {
  89. self.titleEdgeInsets = UIEdgeInsetsMake((selfHeight - padding - titleRect.origin.y - titleRect.size.height),
  90. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  91. -(selfHeight - padding - titleRect.origin.y - titleRect.size.height),
  92. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  93. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  94. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  95. 0,
  96. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  97. }
  98. break;
  99. case ButtonImageTitleStyleCenterUp:
  100. {
  101. self.titleEdgeInsets = UIEdgeInsetsMake(-(titleRect.origin.y + titleRect.size.height - imageRect.origin.y + padding),
  102. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  103. (titleRect.origin.y + titleRect.size.height - imageRect.origin.y + padding),
  104. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  105. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  106. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  107. 0,
  108. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  109. }
  110. break;
  111. case ButtonImageTitleStyleCenterDown:
  112. {
  113. self.titleEdgeInsets = UIEdgeInsetsMake((imageRect.origin.y + imageRect.size.height - titleRect.origin.y + padding),
  114. (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
  115. -(imageRect.origin.y + imageRect.size.height - titleRect.origin.y + padding),
  116. -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
  117. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  118. (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
  119. 0,
  120. -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
  121. }
  122. break;
  123. case ButtonImageTitleStyleRightLeft:
  124. {
  125. //图片在右,文字在左,距离按钮两边边距
  126. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  127. -(titleRect.origin.x - padding),
  128. 0,
  129. (titleRect.origin.x - padding));
  130. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  131. (selfWidth - padding - imageRect.origin.x - imageRect.size.width),
  132. 0,
  133. -(selfWidth - padding - imageRect.origin.x - imageRect.size.width));
  134. }
  135. break;
  136. case ButtonImageTitleStyleLeftRight:
  137. {
  138. //图片在左,文字在右,距离按钮两边边距
  139. self.titleEdgeInsets = UIEdgeInsetsMake(0,
  140. (selfWidth - padding - titleRect.origin.x - titleRect.size.width),
  141. 0,
  142. -(selfWidth - padding - titleRect.origin.x - titleRect.size.width));
  143. self.imageEdgeInsets = UIEdgeInsetsMake(0,
  144. -(imageRect.origin.x - padding),
  145. 0,
  146. (imageRect.origin.x - padding));
  147. }
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. else {
  154. self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  155. self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  156. }
  157. }
  158. @end