RABadgeNumberView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // RABadgeNumberView.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/9/3.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RABadgeNumberView.h"
  9. #define Max_Width 30.0f
  10. #define Min_Width 16.0f
  11. #define Text_Size 12.0f
  12. @interface RABadgeNumberView ()
  13. {
  14. UIColor *_backgroundColor;
  15. UIColor *_foregroundColor;
  16. }
  17. @property (nonatomic,strong) CAShapeLayer *originLayer;
  18. @property (nonatomic,strong) CAShapeLayer *borderLayer;
  19. @property (nonatomic,strong) CATextLayer *valueLayer;
  20. @end
  21. @implementation RABadgeNumberView
  22. #pragma mark - Utils
  23. + (CGSize)sizeOfString:(NSString *)string font:(UIFont *)font{
  24. if (string == nil) {
  25. return CGSizeZero;
  26. }
  27. NSDictionary *attribute = @{NSFontAttributeName: font};
  28. CGSize resSize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
  29. options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
  30. attributes:attribute
  31. context:nil].size;
  32. return resSize;
  33. }
  34. #pragma mark - Public
  35. - (void)setBadgeNumber:(NSUInteger)badgeNumber {
  36. _badgeNumber = badgeNumber;
  37. self.hidden = _badgeNumber == 0;
  38. NSString *text = nil;
  39. if (_badgeNumber > 99) {
  40. text = @"99+";
  41. } else {
  42. text = [NSString stringWithFormat:@"%lu",badgeNumber];
  43. }
  44. [self.valueLayer setString:text];
  45. [self updateUI];
  46. }
  47. - (void)setForegroundColor:(UIColor *)foregroundColor {
  48. _foregroundColor = foregroundColor;
  49. self.valueLayer.foregroundColor = _foregroundColor.CGColor;
  50. }
  51. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  52. _backgroundColor = backgroundColor;
  53. [self updateColor];
  54. }
  55. #pragma mark - Private
  56. - (UIColor *)foregroundColor {
  57. if (_foregroundColor == nil) {
  58. _foregroundColor = [UIColor whiteColor];
  59. }
  60. return _foregroundColor;
  61. }
  62. - (UIColor *)backgroundColor {
  63. if (_backgroundColor == nil) {
  64. _backgroundColor = [UIColor redColor];
  65. }
  66. return _backgroundColor;
  67. }
  68. - (void)willMoveToSuperview:(UIView *)newSuperview {
  69. [super willMoveToSuperview:newSuperview];
  70. [self.layer addSublayer:self.originLayer];
  71. [self.layer addSublayer:self.borderLayer];
  72. [self.layer addSublayer:self.valueLayer];
  73. [self updateUI];
  74. }
  75. - (void)layoutSubviews {
  76. [super layoutSubviews];
  77. [self updateUI];
  78. }
  79. #pragma mark Frame
  80. - (void)setWidth:(CGFloat)width {
  81. NSLayoutConstraint *widthConstraint = nil;
  82. for (NSLayoutConstraint *constraint in self.constraints) {
  83. if (constraint.firstAttribute == NSLayoutAttributeWidth) {
  84. widthConstraint = constraint;
  85. }
  86. }
  87. if (widthConstraint) {
  88. widthConstraint.constant = width;
  89. } else {
  90. CGRect bounds = self.bounds;
  91. bounds.size.width = width;
  92. self.bounds = bounds;
  93. }
  94. }
  95. - (void)setHeight:(CGFloat)height {
  96. NSLayoutConstraint *heightConstraint = nil;
  97. for (NSLayoutConstraint *constraint in self.constraints) {
  98. if (constraint.firstAttribute == NSLayoutAttributeHeight) {
  99. heightConstraint = constraint;
  100. }
  101. }
  102. if (heightConstraint) {
  103. heightConstraint.constant = height;
  104. } else {
  105. CGRect bounds = self.bounds;
  106. bounds.size.height = height;
  107. self.bounds = bounds;
  108. }
  109. }
  110. - (void)updateUI {
  111. CGSize size = [self.class sizeOfString:self.valueLayer.string font:[UIFont systemFontOfSize:Text_Size]];
  112. CGSize textSize = size;
  113. if (size.width < Min_Width) {
  114. size.width = Min_Width;
  115. }
  116. if (size.height < Min_Width) {
  117. size.height = Min_Width;
  118. }
  119. if (size.height > size.width) {
  120. size.width = size.height;
  121. }
  122. size.width += 5.0f;
  123. size.height += 5.0f;
  124. CGFloat r = size.height * 0.5;
  125. // 更新自身Size
  126. if (self.badgeNumber < 10) {
  127. [self setWidth:size.width];
  128. } else {
  129. [self setWidth:size.width + 5];
  130. }
  131. [self setHeight:size.height];
  132. // drag layer
  133. self.borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:r].CGPath;
  134. // 更新TextLayer Frame
  135. CGFloat x = (CGRectGetWidth(self.bounds) - textSize.width) * 0.5,y = (CGRectGetHeight(self.bounds) - textSize.height) * 0.5;
  136. self.valueLayer.frame = CGRectMake(x, y, textSize.width, textSize.height);
  137. }
  138. - (void)updateColor {
  139. self.originLayer.fillColor = self.backgroundColor.CGColor;
  140. self.originLayer.strokeColor = self.backgroundColor.CGColor;
  141. self.borderLayer.fillColor = self.backgroundColor.CGColor;
  142. self.borderLayer.strokeColor = self.backgroundColor.CGColor;
  143. }
  144. #pragma mark Layer
  145. - (CAShapeLayer *)originLayer {
  146. if (!_originLayer) {
  147. _originLayer = [CAShapeLayer layer];
  148. _originLayer.fillColor = self.backgroundColor.CGColor;
  149. _originLayer.strokeColor = self.backgroundColor.CGColor;
  150. _originLayer.lineWidth = 0.1f;
  151. }
  152. return _originLayer;
  153. }
  154. - (CAShapeLayer *)borderLayer {
  155. if (!_borderLayer) {
  156. _borderLayer = [CAShapeLayer layer];
  157. _borderLayer.fillColor = self.backgroundColor.CGColor;
  158. _borderLayer.strokeColor = self.backgroundColor.CGColor;
  159. _borderLayer.lineWidth = 0.1f;
  160. }
  161. return _borderLayer;
  162. }
  163. - (CATextLayer *)valueLayer {
  164. if (!_valueLayer) {
  165. _valueLayer = [CATextLayer layer];
  166. _valueLayer.foregroundColor = self.foregroundColor.CGColor;
  167. _valueLayer.contentsScale = [UIScreen mainScreen].scale;
  168. _valueLayer.alignmentMode = kCAAlignmentCenter;
  169. _valueLayer.contentsGravity = kCAGravityCenter;
  170. UIFont *font = [UIFont systemFontOfSize:Text_Size];
  171. CGFontRef fontRef = CGFontCreateWithFontName((__bridge_retained CFStringRef)font.fontName);
  172. _valueLayer.font = fontRef;
  173. _valueLayer.fontSize = font.pointSize;
  174. CGFontRelease(fontRef);
  175. }
  176. return _valueLayer;
  177. }
  178. @end