TDButton.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // TDButton.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/20.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "TDButton.h"
  9. @interface TDButton ()
  10. @property (nonatomic) CGSize hotZoneSize;
  11. @property (nonatomic, assign) BOOL hasCustHotZone;
  12. @property (nonatomic, strong) UILabel *badgeLabel;
  13. @end
  14. @implementation TDButton
  15. - (instancetype)init
  16. {
  17. self = [super init];
  18. if (self) {
  19. self.adjustsImageWhenHighlighted = NO;
  20. }
  21. return self;
  22. }
  23. - (void)setCurrentButtonHotSize:(CGSize)size {
  24. self.hasCustHotZone = YES;
  25. //热区不应小于44x44
  26. if (size.width < 44) {
  27. size.width = 44;
  28. }
  29. if (size.height < 44) {
  30. size.height = 44;
  31. }
  32. _hotZoneSize = size;
  33. }
  34. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
  35. if (self.hasCustHotZone) {
  36. CGRect bounds = self.bounds;
  37. CGFloat widthDelta = MAX(self.hotZoneSize.width - bounds.size.width, 0);
  38. CGFloat heightDelta = MAX(self.hotZoneSize.height - bounds.size.height, 0);
  39. bounds = CGRectInset(bounds, - 0.5 * widthDelta, - 0.5 * heightDelta);
  40. return CGRectContainsPoint(bounds, point);
  41. }
  42. return [super pointInside:point withEvent:event];
  43. }
  44. - (void)verticalImageAndTitle:(CGFloat)spacing {
  45. CGSize imageSize = self.imageView.frame.size;
  46. CGSize titleSize = self.titleLabel.frame.size;
  47. CGSize textSize = [self.titleLabel sizeThatFits:CGSizeZero];
  48. CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
  49. if (titleSize.width + 0.5 < frameSize.width) {
  50. titleSize.width = frameSize.width;
  51. }
  52. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
  53. self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
  54. self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
  55. }
  56. - (void)setButtonAlpha:(CGFloat)alpha {
  57. [UIView animateWithDuration:0.2 animations:^{
  58. self.alpha = alpha;
  59. }];
  60. }
  61. - (void)setButtonBadge:(NSInteger)num isSelect:(BOOL)isSelect{
  62. [self addSubview:self.badgeLabel];
  63. _badgeLabel.textColor = isSelect ? UIColorHex(168BFF): UIColorHex(999999);
  64. _badgeLabel.font = [UIFont systemFontOfSize:9.f];
  65. _badgeLabel.text = [NSString stringWithFormat:@"%ld", (long)num];
  66. CGSize badgeSize = [_badgeLabel sizeThatFits:CGSizeZero];
  67. _badgeLabel.frame = CGRectMake(self.frame.size.width-2.5, -4.8, badgeSize.width, badgeSize.height);
  68. _badgeLabel.hidden = num == 0 ? YES : NO;
  69. }
  70. - (void)setButtonBadge:(NSInteger)num {
  71. [self addSubview:self.badgeLabel];
  72. _badgeLabel.textColor = UIColorHex(168BFF);
  73. _badgeLabel.font = [UIFont systemFontOfSize:9.f];
  74. _badgeLabel.text = [NSString stringWithFormat:@"%ld", (long)num];
  75. CGSize badgeSize = [_badgeLabel sizeThatFits:CGSizeZero];
  76. _badgeLabel.frame = CGRectMake(self.frame.size.width-2.5, -4.8, badgeSize.width, badgeSize.height);
  77. _badgeLabel.hidden = num == 0 ? YES : NO;
  78. }
  79. - (UILabel *)badgeLabel {
  80. if (!_badgeLabel) {
  81. _badgeLabel = [UILabel new];
  82. }
  83. return _badgeLabel;
  84. }
  85. #pragma mark - otherLayout
  86. - (void)layoutButtonWithEdgeInsetsStyle:(GLButtonEdgeInsetsStyle)style
  87. imageTitleSpace:(CGFloat)space {
  88. // 1. 得到imageView和titleLabel的宽、高
  89. CGFloat imageWith = self.imageView.image.size.width;
  90. CGFloat imageHeight = self.imageView.image.size.height;
  91. CGFloat labelWidth = 0.0;
  92. CGFloat labelHeight = 0.0;
  93. if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
  94. // 由于iOS8中titleLabel的size为0,用下面的这种设置
  95. labelWidth = self.titleLabel.intrinsicContentSize.width;
  96. labelHeight = self.titleLabel.intrinsicContentSize.height;
  97. } else {
  98. labelWidth = self.titleLabel.frame.size.width;
  99. labelHeight = self.titleLabel.frame.size.height;
  100. }
  101. // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
  102. UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
  103. UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
  104. // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
  105. switch (style) {
  106. case GLButtonEdgeInsetsStyleTop:
  107. {
  108. imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
  109. labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
  110. }
  111. break;
  112. case GLButtonEdgeInsetsStyleLeft:
  113. {
  114. imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
  115. labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
  116. }
  117. break;
  118. case GLButtonEdgeInsetsStyleBottom:
  119. {
  120. imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
  121. labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
  122. }
  123. break;
  124. case GLButtonEdgeInsetsStyleRight:
  125. {
  126. imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
  127. labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. // 4. 赋值
  134. self.titleEdgeInsets = labelEdgeInsets;
  135. self.imageEdgeInsets = imageEdgeInsets;
  136. }
  137. @end