UIView+JYPBadgeView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // UIView+JYPBadgeView.m
  3. // QuanFangTong
  4. //
  5. // Created by apple on 2018/1/3.
  6. // Copyright © 2018年 xinkai. All rights reserved.
  7. //
  8. #define BAGEVIEW_HEIGHT 17
  9. #define BAGEVIEW_WIDTH1 17
  10. #define BAGEVIEW_WIDTH2 25
  11. #define BAGEVIEW_WIDTH3 35
  12. #import "UIView+JYPBadgeView.h"
  13. #import "UIView+XMGExtension.h"
  14. #import "UIView+ViewController.h"
  15. #import "View+MASAdditions.h"
  16. @implementation XKBadgeView
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. [self createUI];
  22. }
  23. return self;
  24. }
  25. - (void)createUI {
  26. self.imageView = [[UIImageView alloc] init];
  27. [self addSubview:self.imageView];
  28. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.top.left.bottom.right.equalTo(self);
  30. }];
  31. self.valueLab = [[UILabel alloc] init];
  32. self.valueLab.textAlignment = NSTextAlignmentCenter;
  33. self.valueLab.textColor = [UIColor whiteColor];
  34. self.valueLab.font = [UIFont systemFontOfSize:11.0];
  35. [self.imageView addSubview:self.valueLab];
  36. [self.valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.top.left.bottom.right.equalTo(self.imageView);
  38. }];
  39. }
  40. - (void)setBValue:(NSString *)bValue
  41. {
  42. _bValue = bValue;
  43. if(_bValue.length > 3){
  44. _bValue = @"99+";
  45. }
  46. CGFloat bageValueW = BAGEVIEW_HEIGHT;
  47. CGFloat bageValueH = BAGEVIEW_HEIGHT;
  48. self.valueLab.text = _bValue;
  49. // self.valueLab.text = @"";
  50. if (self.bValue.length == 1) {
  51. bageValueW = bageValueH;
  52. self.imageView.image = [UIImage imageNamed:@"bage_one"];
  53. }else if (self.bValue.length == 2){
  54. bageValueW = BAGEVIEW_WIDTH2;
  55. self.imageView.image = [UIImage imageNamed:@"bage_two"];
  56. }else{
  57. bageValueW = BAGEVIEW_WIDTH3;
  58. self.imageView.image = [UIImage imageNamed:@"bage_three"];
  59. }
  60. // self.imageView.image = [UIImage imageNamed:@"Home_红点"];
  61. self.width = bageValueW;
  62. self.height = bageValueH;
  63. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  64. make.width.mas_equalTo(bageValueW);
  65. make.height.mas_equalTo(bageValueH);
  66. }];
  67. self.layer.cornerRadius = bageValueH/2;//圆形
  68. }
  69. @end
  70. @class XKBadgeView;
  71. @implementation UIView (JYPBadgeView)
  72. - (void)setBadgeValue:(NSString *)value title:(NSString *)title
  73. {
  74. XKBadgeView *badgeView = [[XKBadgeView alloc] initWithFrame:CGRectZero];
  75. [self addSubview:badgeView];
  76. // 确定小红点的位置
  77. [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.top.mas_equalTo(-(BAGEVIEW_HEIGHT/2));
  79. make.left.equalTo(self.mas_right).offset(0);
  80. make.width.height.mas_equalTo(BAGEVIEW_HEIGHT);
  81. }];
  82. if (![value isEqualToString:@"0"] && value != nil && value .length > 0 && [value integerValue] > 0) {
  83. badgeView.bValue = [NSString stringWithFormat:@"%@%@",title ,value];
  84. }else{
  85. badgeView.hidden = YES;
  86. [self removeAllSubviews];
  87. }
  88. }
  89. - (void)setBadgeValue:(NSString *)value
  90. {
  91. XKBadgeView *badgeView = [[XKBadgeView alloc] initWithFrame:CGRectZero];
  92. [self addSubview:badgeView];
  93. // 确定小红点的位置
  94. [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.top.mas_equalTo(-(BAGEVIEW_HEIGHT/2));
  96. make.left.equalTo(self.mas_right).offset(0);
  97. make.width.height.mas_equalTo(BAGEVIEW_HEIGHT);
  98. }];
  99. if (![value isEqualToString:@"0"] && value != nil && value .length > 0 && [value integerValue] > 0) {
  100. badgeView.bValue = value;
  101. }else{
  102. badgeView.hidden = YES;
  103. [self removeAllSubviews];
  104. }
  105. }
  106. - (void)setBadgeValue1:(NSString *)value
  107. {
  108. XKBadgeView *badgeView = [[XKBadgeView alloc] initWithFrame:CGRectZero];
  109. [self addSubview:badgeView];
  110. // 确定小红点的位置
  111. [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.top.mas_equalTo(BAGEVIEW_HEIGHT/3);
  113. make.left.equalTo(self.mas_right).offset(-BAGEVIEW_HEIGHT/3);
  114. make.width.height.mas_equalTo(BAGEVIEW_HEIGHT);
  115. }];
  116. if (![value isEqualToString:@"0"] && value != nil && value .length > 0 && [value integerValue] > 0) {
  117. badgeView.bValue = value;
  118. }else{
  119. badgeView.hidden = YES;
  120. [self removeAllSubviews];
  121. }
  122. }
  123. @end