CustomButton.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // customButton.m
  3. // 项目-iWeiBo
  4. //
  5. // Created by student on 14-9-16.
  6. // Copyright (c) 2014年 student. All rights reserved.
  7. //
  8. #import "CustomButton.h"
  9. #define imageToTextRatio 0.3
  10. @implementation CustomButton
  11. - (instancetype)init
  12. {
  13. if (self = [super init]) {
  14. self.titleLabel.textColor = [UIColor blackColor];
  15. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  16. }
  17. return self;
  18. }
  19. - (id)initCustomBtnWithImageDirection:(ImagePostion)imagePosition imageViewSize:(CGSize)imageSize imageLabelMargin:(CGFloat)imageLabelMargin titleLabelFont:(UIFont *)titleLabelFont imageIcon:(NSString *)imageIcon selectIcon:(NSString *)selectIcon title:(NSString *)title btnWidth:(CGFloat)btnWidth
  20. {
  21. if (self = [super init]) {
  22. self.titleLabel.numberOfLines = 0;
  23. [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  24. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  25. // self.imageView.backgroundColor = [UIColor redColor];
  26. self.layoutType = LayoutTypeMargin;
  27. self.imagePostion = imagePosition;
  28. self.imageSize = imageSize;
  29. self.titleLabelFont = titleLabelFont;
  30. self.imageLabelMargin = imageLabelMargin;
  31. [self setImage:[UIImage imageNamed:imageIcon] forState:UIControlStateNormal];
  32. if (selectIcon) {
  33. [self setImage:[UIImage imageNamed:selectIcon] forState:UIControlStateSelected];
  34. }
  35. // [self setBackgroundImage:[UIImage imageNamed:imageIcon] forState:UIControlStateNormal];
  36. [self setTitle:title forState:UIControlStateNormal];
  37. [self setFont:titleLabelFont];
  38. //top 方向的有用.
  39. if (imagePosition == ImagePostionTop) {
  40. self.customBtnHeight = self.imageSize.height + self.imageLabelMargin + [title stringgetSizebyMaxWidth:btnWidth Font:titleLabelFont].height;
  41. }else if (imagePosition == ImagePostionLeft)
  42. {
  43. self.customBtnHeight = imageSize.height;
  44. }
  45. }
  46. return self;
  47. }
  48. - (void)setTitle:(nullable NSString *)title forState:(UIControlState)state
  49. {
  50. [super setTitle:title forState:state];
  51. //存文本的..
  52. if(self.imagePostion == ImagePostionNoImage){
  53. CGSize newSize = [self labelAutoCalculateRectWith:title FontSize:self.titleLabel.font.pointSize MaxSize:CGSizeMake(100, 20)];
  54. self.bounds = CGRectMake(0, 0, newSize.width , newSize.height);
  55. }else if (self.imagePostion == ImagePostionLeft)
  56. {
  57. CGSize newSize = [self labelAutoCalculateRectWith:title FontSize:self.titleLabel.font.pointSize MaxSize:CGSizeMake(SCREEN_WIDTH/2.0f, 40)];
  58. self.bounds = CGRectMake(0, 0, newSize.width + 2 * newSize.height , newSize.height);
  59. }
  60. }
  61. - (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
  62. {
  63. NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  64. paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
  65. NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
  66. CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
  67. labelSize.height=ceil(labelSize.height);
  68. labelSize.width=ceil(labelSize.width);
  69. return labelSize;
  70. }
  71. //- (id)initWithFrame:(CGRect)frame
  72. //{
  73. // self = [super initWithFrame:frame];
  74. // if (self) {
  75. // // Initialization code
  76. // self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  77. // self.titleLabel.textAlignment = NSTextAlignmentCenter;
  78. // self.titleLabel.font = TabbarButtonTextFont;
  79. //
  80. // [self setTitleColor: [UIColor colorWithRed:248/255.0 green:139/255.0 blue:0/255.0 alpha:1] forState:UIControlStateSelected];
  81. //
  82. // //添加unreadbutton
  83. // }
  84. // return self;
  85. //}
  86. //- (void)setHighlighted:(BOOL)highlighted
  87. //{
  88. //
  89. //}
  90. - (CGRect)imageRectForContentRect:(CGRect)contentRect
  91. {
  92. if (self.layoutType == LayoutTypeMargin) {
  93. if (self.imagePostion == ImagePostionTop){
  94. CGFloat imageX = self.frame.size.width/2.0f - self.imageSize.width/2.0f;
  95. CGFloat imageY = 0;
  96. return CGRectMake(imageX, imageY, self.imageSize.width, self.imageSize.height);
  97. }else if (self.imagePostion == ImagePostionLeft){
  98. CGFloat imageX = 0;
  99. CGFloat imageY = (self.frame.size.height-self.imageSize.height)/2.0f;
  100. return CGRectMake(imageX, imageY, self.imageSize.width, self.imageSize.height);
  101. }
  102. else
  103. return [super imageRectForContentRect:contentRect];
  104. }else{
  105. if (self.imagePostion == ImagePostionLeft) {
  106. CGFloat imageW = self.frame.size.width * imageToTextRatio;
  107. return CGRectMake(0, self.frame.size.height/2.0f - imageW/2.0f, imageW, imageW);
  108. }else if (self.imagePostion == ImagePostionTop)
  109. {
  110. CGFloat imageX = self.frame.size.width/2.0f - self.imageSize.width/2.0f;
  111. CGFloat imageY = 0;
  112. CGFloat imageW = self.frame.size.width;
  113. CGFloat imageH = self.frame.size.height * (1 - (self.imageToTextRatioFloat?self.imageToTextRatioFloat:imageToTextRatio));
  114. return CGRectMake(imageX, imageY, imageW, imageH);
  115. }else if (self.imagePostion == ImagePostionCustom)
  116. {
  117. CGFloat imageX = self.imageFrame.origin.x;
  118. CGFloat imageY = self.frame.size.height/2.0f - self.imageFrame.size.height/2.0f;
  119. CGFloat imageW = self.imageFrame.size.width;
  120. CGFloat imageH = self.imageFrame.size.height;
  121. return CGRectMake(imageX, imageY, imageW, imageH);
  122. }
  123. else return [super imageRectForContentRect:contentRect];
  124. }
  125. }
  126. - (CGRect)titleRectForContentRect:(CGRect)contentRect
  127. {
  128. if (self.layoutType == LayoutTypeMargin) {
  129. if (self.imagePostion == ImagePostionTop) {
  130. CGFloat titleLabelX = 0;
  131. CGFloat titleLabelY = self.imageSize.height + self.imageLabelMargin;
  132. CGSize titleLabelSize = [self.titleLabel.text stringgetSizebySize:CGSizeMake(self.frame.size.width, self.frame.size.height - self.imageSize.height - self.imageLabelMargin) Font:self.titleLabelFont];
  133. return CGRectMake(titleLabelX, titleLabelY, self.frame.size.width, titleLabelSize.height? titleLabelSize.height:self.frame.size.height - self.imageSize.height - self.imageLabelMargin);//底部间距.
  134. }else if (self.imagePostion == ImagePostionLeft) {
  135. CGFloat titleLabelX = self.imageLabelMargin + self.imageSize.width;
  136. CGFloat titleLabelY = 0;
  137. CGSize titleLabelSize = [self.titleLabel.text stringgetSizebyMaxWidth:self.frame.size.width - titleLabelX Font:self.titleLabelFont];
  138. return CGRectMake(titleLabelX, titleLabelY, titleLabelSize.width, self.frame.size.height);
  139. }else return [super titleRectForContentRect:contentRect];
  140. }else{
  141. if (self.imagePostion == ImagePostionLeft) {
  142. CGFloat margin = 5;
  143. CGFloat titleX = self.frame.size.width * imageToTextRatio + margin ;
  144. CGFloat titleW = self.frame.size.width * (1-imageToTextRatio) - margin ;
  145. return CGRectMake(titleX, 0, titleW, self.frame.size.height);
  146. }else if (self.imagePostion == ImagePostionTop)
  147. {
  148. CGFloat titleY = self.frame.size.height * (1 - (self.imageToTextRatioFloat?self.imageToTextRatioFloat:imageToTextRatio));
  149. CGFloat titleW = self.frame.size.width;
  150. CGFloat titleH = self.frame.size.height * (self.imageToTextRatioFloat?self.imageToTextRatioFloat:imageToTextRatio);
  151. return CGRectMake(0, titleY , titleW, titleH);
  152. }else if (self.imagePostion == ImagePostionCustom)
  153. {
  154. CGFloat titleX = CGRectGetMaxX(self.imageFrame);
  155. CGFloat titleY = 0;
  156. CGFloat titleW = self.frame.size.width - titleX;
  157. CGFloat titleH = self.frame.size.height;
  158. return CGRectMake(titleX, titleY , titleW, titleH);
  159. }
  160. else {
  161. CGRect rect = contentRect;
  162. rect.size.width = self.bounds.size.width;
  163. contentRect = rect;
  164. return [super titleRectForContentRect:contentRect];
  165. }
  166. }
  167. }
  168. - (void)setImageFrame:(CGRect)imageFrame
  169. {
  170. _imageFrame = imageFrame;
  171. // [self setNeedsLayout];
  172. }
  173. - (void)layoutSubviews
  174. {
  175. [super layoutSubviews];
  176. if (self.isClickCycle) {
  177. self.imageView.layer.masksToBounds = YES;
  178. self.imageView.layer.cornerRadius = self.imageView.frame.size.height/2.0f;
  179. }
  180. // self.titleLabel.backgroundColor =[UIColor redColor];
  181. }
  182. /*
  183. // Only override drawRect: if you perform custom drawing.
  184. // An empty implementation adversely affects performance during animation.
  185. - (void)drawRect:(CGRect)rect
  186. {
  187. // Drawing code
  188. }
  189. */
  190. @end