《省钱达人》与《猎豆优选》UI相同版。域名tbk

UIView+BABadgeView.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // UIView+BABadgeView.m
  3. // BAKit
  4. //
  5. // Created by boai on 2017/7/29.
  6. // Copyright © 2017年 boai. All rights reserved.
  7. //
  8. #import "UIView+BABadgeView.h"
  9. #import "BABadgeLabel.h"
  10. #define BAKit_Objc_setObj(key, value) objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  11. #define BAKit_Objc_getObj objc_getAssociatedObject(self, _cmd)
  12. @interface UIView ()
  13. @property (nonatomic, strong) BABadgeLabel *badgeLabel;
  14. @end
  15. @implementation UIView (BABadgeView)
  16. - (void)ba_addBadgeWithText:(NSString *)text
  17. {
  18. [self lazyLoadBadgeLabel];
  19. [self ba_showBadge];
  20. self.badgeLabel.text = text;
  21. self.badgeLabel.hidden = !text.length;
  22. }
  23. - (void)ba_addBadgeWithNumber:(NSInteger)number
  24. {
  25. if (number <= 0)
  26. {
  27. [self ba_addBadgeWithText:@"0"];
  28. [self ba_hiddenBadge];
  29. return;
  30. }
  31. [self ba_addBadgeWithText:[NSString stringWithFormat:@"%ld",number]];
  32. }
  33. - (void)ba_addDotWithColor:(UIColor *)color
  34. {
  35. [self ba_addBadgeWithText:nil];
  36. [self ba_setBadgeHeightPoints:8];
  37. if (color)
  38. {
  39. self.badgeLabel.backgroundColor = color;
  40. }
  41. }
  42. - (void)ba_moveBadgeWithX:(CGFloat)x Y:(CGFloat)y
  43. {
  44. [self lazyLoadBadgeLabel];
  45. /**
  46. self.badgeLabel.center = CGPointMake(self.width+x, y);
  47. 如果通过 badge 的 center 来调整其在父视图的位置, 在给 badge 赋值不同长度的内容时
  48. 会导致 badge 会以中心点向两边调整其自身宽度,如果badge过长会遮挡部分父视图, 所以
  49. 正确的方式是以 badge 的x坐标为起点,其宽度向x轴正方向增加/x轴负方向减少
  50. */
  51. self.badgeLabel.x = (self.width - self.badgeLabel.height * 0.5)/*badge的x坐标*/ + x;
  52. self.badgeLabel.y = -self.badgeLabel.height * 0.5/*badge的y坐标*/ + y;
  53. }
  54. - (void)ba_setBadgeLabelAttributes:(void (^)(BABadgeLabel *))badgeLabel
  55. {
  56. [self lazyLoadBadgeLabel];
  57. badgeLabel ? badgeLabel(self.badgeLabel) : nil;
  58. }
  59. - (void)ba_setBadgeHeightPoints:(CGFloat)points
  60. {
  61. CGFloat scale = points/self.badgeLabel.height;
  62. self.badgeLabel.transform = CGAffineTransformScale(self.badgeLabel.transform, scale, scale);
  63. }
  64. - (void)ba_showBadge
  65. {
  66. self.badgeLabel.hidden = NO;
  67. }
  68. - (void)ba_hiddenBadge
  69. {
  70. self.badgeLabel.hidden = YES;
  71. }
  72. - (void)ba_increase
  73. {
  74. [self ba_increaseBy:1];
  75. }
  76. - (void)ba_increaseBy:(NSInteger)number
  77. {
  78. NSInteger result = self.badgeLabel.text.integerValue + number;
  79. if (result > 0)
  80. {
  81. [self ba_showBadge];
  82. }
  83. self.badgeLabel.text = [NSString stringWithFormat:@"%ld",result];
  84. }
  85. - (void)ba_decrease
  86. {
  87. [self ba_decreaseBy:1];
  88. }
  89. - (void)ba_decreaseBy:(NSInteger)number
  90. {
  91. NSInteger result = self.badgeLabel.text.integerValue - number;
  92. if (result <= 0)
  93. {
  94. [self ba_hiddenBadge];
  95. self.badgeLabel.text = @"0";
  96. return;
  97. }
  98. self.badgeLabel.text = [NSString stringWithFormat:@"%ld",result];
  99. }
  100. - (void)lazyLoadBadgeLabel
  101. {
  102. if (!self.badgeLabel)
  103. {
  104. self.badgeLabel = [BABadgeLabel ba_badgeLabelDefaultBadgeLabel];
  105. self.badgeLabel.center = CGPointMake(self.width, 0);
  106. [self addSubview:self.badgeLabel];
  107. [self bringSubviewToFront:self.badgeLabel];
  108. }
  109. }
  110. #pragma mark - setter/getter
  111. - (BABadgeLabel *)badgeLabel
  112. {
  113. return BAKit_Objc_getObj;
  114. }
  115. - (void)setBadgeLabel:(BABadgeLabel *)badgeLabel
  116. {
  117. BAKit_Objc_setObj(@selector(badgeLabel), badgeLabel);
  118. }
  119. @end