NIMBadgeView.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // NIMBadgeView.m
  3. // NIMKit
  4. //
  5. // Created by chris on 15/2/12.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NIMBadgeView.h"
  9. #import "NSString+NIMKit.h"
  10. @interface NIMBadgeView ()
  11. //
  12. //@property (strong) UIColor *badgeTextColor;
  13. //
  14. //@property (nonatomic) UIFont *badgeTextFont;
  15. @property (nonatomic) CGFloat badgeTopPadding; //数字顶部到红圈的距离
  16. @property (nonatomic) CGFloat badgeLeftPadding; //数字左部到红圈的距离
  17. @property (nonatomic) CGFloat whiteCircleWidth; //最外层白圈的宽度
  18. @end
  19. @implementation NIMBadgeView
  20. + (instancetype)viewWithBadgeTip:(NSString *)badgeValue{
  21. if (!badgeValue) {
  22. badgeValue = @"";
  23. }
  24. NIMBadgeView *instance = [[NIMBadgeView alloc] init];
  25. instance.frame = [instance frameWithStr:badgeValue];
  26. instance.badgeValue = badgeValue;
  27. // instance.badgeBackgroundColor = HexColorFromRGB(0xFE3A3A);
  28. return instance;
  29. }
  30. - (id)initWithFrame:(CGRect)frame {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. self.backgroundColor = [UIColor clearColor];
  34. _badgeBackgroundColor = self.badgeBackgroundColor;
  35. _badgeTextColor = [UIColor whiteColor];
  36. _badgeTextFont = [UIFont boldSystemFontOfSize:12];
  37. _whiteCircleWidth = 2.f;
  38. _CirclegroundColor = self.CirclegroundColor;
  39. }
  40. return self;
  41. }
  42. - (void)drawRect:(CGRect)rect
  43. {
  44. CGContextRef context = UIGraphicsGetCurrentContext();
  45. CGContextSaveGState(context);
  46. if ([[self badgeValue] length]) {
  47. [self drawWithContent:rect context:context];
  48. }else{
  49. [self drawWithOutContent:rect context:context];
  50. }
  51. CGContextRestoreGState(context);
  52. }
  53. - (void)setBadgeValue:(NSString *)badgeValue {
  54. _badgeValue = badgeValue;
  55. if (_badgeValue.integerValue > 9) {
  56. _badgeLeftPadding = 4.f;
  57. }else{
  58. _badgeLeftPadding = 0.f;
  59. }
  60. _badgeTopPadding = 0.f;
  61. self.frame = [self frameWithStr:badgeValue];
  62. [self setNeedsDisplay];
  63. }
  64. - (CGSize)badgeSizeWithStr:(NSString *)badgeValue{
  65. if (!badgeValue || badgeValue.length == 0) {
  66. return CGSizeZero;
  67. }
  68. CGSize size = [badgeValue sizeWithAttributes:@{NSFontAttributeName:self.badgeTextFont}];
  69. if (size.width < size.height) {
  70. size = CGSizeMake(size.height, size.height);
  71. }
  72. return size;
  73. }
  74. - (CGRect)frameWithStr:(NSString *)badgeValue{
  75. CGSize badgeSize = [self badgeSizeWithStr:badgeValue];
  76. CGRect badgeFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, badgeSize.width + self.badgeLeftPadding * 2 + self.whiteCircleWidth * 2, badgeSize.height + self.badgeTopPadding * 2 + self.whiteCircleWidth * 2);//8=2*2(红圈-文字)+2*2(白圈-红圈)
  77. return badgeFrame;
  78. }
  79. #pragma mark - Private
  80. - (void)drawWithContent:(CGRect)rect context:(CGContextRef)context{
  81. CGRect bodyFrame = self.bounds;
  82. CGRect bkgFrame = CGRectInset(self.bounds, self.whiteCircleWidth, self.whiteCircleWidth);
  83. CGRect badgeSize = CGRectInset(self.bounds, self.whiteCircleWidth + self.badgeLeftPadding, self.whiteCircleWidth + self.badgeTopPadding);
  84. if ([self badgeBackgroundColor]) {//外白色描边
  85. CGContextSetFillColorWithColor(context, [self.CirclegroundColor CGColor]);
  86. if ([self badgeValue].integerValue > 9) {
  87. CGFloat circleWith = bodyFrame.size.height;
  88. CGFloat totalWidth = bodyFrame.size.width;
  89. CGFloat diffWidth = totalWidth - circleWith;
  90. CGPoint originPoint = bodyFrame.origin;
  91. CGRect leftCicleFrame = CGRectMake(originPoint.x, originPoint.y, circleWith, circleWith);
  92. CGRect centerFrame = CGRectMake(originPoint.x +circleWith/2, originPoint.y, diffWidth, circleWith);
  93. CGRect rightCicleFrame = CGRectMake(originPoint.x +(totalWidth - circleWith), originPoint.y, circleWith, circleWith);
  94. CGContextFillEllipseInRect(context, leftCicleFrame);
  95. CGContextFillRect(context, centerFrame);
  96. CGContextFillEllipseInRect(context, rightCicleFrame);
  97. }else{
  98. CGContextFillEllipseInRect(context, bodyFrame);
  99. }
  100. // badge背景色
  101. CGContextSetFillColorWithColor(context, [[self badgeBackgroundColor] CGColor]);
  102. if ([self badgeValue].integerValue > 9) {
  103. CGFloat circleWith = bkgFrame.size.height;
  104. CGFloat totalWidth = bkgFrame.size.width;
  105. CGFloat diffWidth = totalWidth - circleWith;
  106. CGPoint originPoint = bkgFrame.origin;
  107. CGRect leftCicleFrame = CGRectMake(originPoint.x, originPoint.y, circleWith, circleWith);
  108. CGRect centerFrame = CGRectMake(originPoint.x +circleWith/2, originPoint.y, diffWidth, circleWith);
  109. CGRect rightCicleFrame = CGRectMake(originPoint.x +(totalWidth - circleWith), originPoint.y, circleWith, circleWith);
  110. CGContextFillEllipseInRect(context, leftCicleFrame);
  111. CGContextFillRect(context, centerFrame);
  112. CGContextFillEllipseInRect(context, rightCicleFrame);
  113. }else{
  114. CGContextFillEllipseInRect(context, bkgFrame);
  115. }
  116. }
  117. CGContextSetFillColorWithColor(context, [[self badgeTextColor] CGColor]);
  118. NSMutableParagraphStyle *badgeTextStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
  119. [badgeTextStyle setLineBreakMode:NSLineBreakByWordWrapping];
  120. [badgeTextStyle setAlignment:NSTextAlignmentCenter];
  121. NSDictionary *badgeTextAttributes = @{
  122. NSFontAttributeName: [self badgeTextFont],
  123. NSForegroundColorAttributeName: [self badgeTextColor],
  124. NSParagraphStyleAttributeName: badgeTextStyle,
  125. };
  126. [[self badgeValue] drawInRect:CGRectMake(self.whiteCircleWidth + self.badgeLeftPadding,
  127. self.whiteCircleWidth + self.badgeTopPadding,
  128. badgeSize.size.width, badgeSize.size.height)
  129. withAttributes:badgeTextAttributes];
  130. }
  131. - (void)drawWithOutContent:(CGRect)rect context:(CGContextRef)context{
  132. CGRect bodyFrame = self.bounds;
  133. CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
  134. CGContextFillEllipseInRect(context, bodyFrame);
  135. }
  136. @end