YOUPAILZBadgeButton.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // YOUPAILZBadgeButton.m
  3. // TIANYAN
  4. //
  5. // Created by CY on 2021/5/15.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZBadgeButton.h"
  9. @interface YOUPAILZBadgeButton ()
  10. @property (nonatomic, weak) UILabel *youpaipbadgeL;
  11. @end
  12. @implementation YOUPAILZBadgeButton
  13. + (instancetype)buttonWithType:(UIButtonType)buttonType{
  14. YOUPAILZBadgeButton *badgeBtn = [super buttonWithType:buttonType];
  15. [badgeBtn youpaifinitUI];
  16. [badgeBtn youpaifinitialization];
  17. return badgeBtn;
  18. }
  19. - (instancetype)init{
  20. if (self = [super init]) {
  21. [self youpaifinitUI];
  22. [self youpaifinitialization];
  23. }
  24. return self;
  25. }
  26. - (void)youpaifinitialization{
  27. self.youpaipbadgeBgColor = HexColorFromRGB(0xF4003F);
  28. self.youpaipbadgeTextColor = [UIColor whiteColor];
  29. self.youpaipbadgeFont = LCFont(10.0f);
  30. self.youpaipbadgeSize = CGSizeMake(14.0f, 14.0f);
  31. }
  32. - (void)youpaifinitUI{
  33. UILabel *youpaipbadgeL = [[UILabel alloc] init];
  34. youpaipbadgeL.hidden = YES;
  35. youpaipbadgeL.textAlignment = NSTextAlignmentCenter;
  36. [self addSubview:youpaipbadgeL];
  37. self.youpaipbadgeL = youpaipbadgeL;
  38. [youpaipbadgeL mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.equalTo(self.mas_top).offset(0.0f);
  40. make.left.equalTo(self.mas_right).offset(-7.0f-5.0f);
  41. make.width.greaterThanOrEqualTo(0.0f);
  42. make.height.offset(0.0f);
  43. }];
  44. }
  45. - (void)setYoupaipbadgeValue:(NSInteger)badgeValue{
  46. _youpaipbadgeValue = badgeValue;
  47. self.youpaipbadgeL.hidden = badgeValue <= 0;
  48. self.youpaipbadgeL.text = [NSString stringWithFormat:@"%ld",badgeValue];
  49. if (badgeValue > 99) {
  50. self.youpaipbadgeL.text = @"99+";
  51. }
  52. }
  53. - (void)setYoupaipbadgeSize:(CGSize)youpaipbadgeSize{
  54. _youpaipbadgeSize = youpaipbadgeSize;
  55. self.youpaipbadgeL.layer.cornerRadius = youpaipbadgeSize.width/2.0f;
  56. self.youpaipbadgeL.clipsToBounds = YES;
  57. [self.youpaipbadgeL mas_updateConstraints:^(MASConstraintMaker *make) {
  58. make.width.greaterThanOrEqualTo(youpaipbadgeSize.width);
  59. make.height.offset(youpaipbadgeSize.height);
  60. }];
  61. }
  62. - (void)setYoupaipbadgeBgColor:(UIColor *)youpaipbadgeBgColor{
  63. _youpaipbadgeBgColor = youpaipbadgeBgColor;
  64. self.youpaipbadgeL.backgroundColor = youpaipbadgeBgColor;
  65. }
  66. - (void)setYoupaipbadgeTextColor:(UIColor *)youpaipbadgeTextColor{
  67. _youpaipbadgeTextColor = youpaipbadgeTextColor;
  68. self.youpaipbadgeL.textColor = youpaipbadgeTextColor;
  69. }
  70. - (void)setYoupaipbadgeFont:(UIFont *)youpaipbadgeFont{
  71. _youpaipbadgeFont = youpaipbadgeFont;
  72. self.youpaipbadgeL.font = youpaipbadgeFont;
  73. }
  74. @end