RoundedNumberView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "RoundedNumberView.h"
  6. #import "NCAppBranding.h"
  7. #define kRoundedNumberViewImportantBackgroundColor [UIColor colorWithRed:0.00 green:0.51 blue:0.79 alpha:1.0] //#0082C9
  8. #define kRoundedNumberViewImportantTextColor [UIColor whiteColor]
  9. #define kRoundedNumberViewDefaultBackgroundColor [UIColor colorWithRed:0.84 green:0.84 blue:0.84 alpha:1.0] //#d5d5d5
  10. #define kRoundedNumberViewDefaultTextColor [UIColor blackColor]
  11. #define kRoundedNumberViewCounterLimit 9999
  12. @interface RoundedNumberView ()
  13. @property (nonatomic, strong) UILabel *numberLabel;
  14. @end
  15. @implementation RoundedNumberView
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. return [self initWithNumber:0];
  19. }
  20. - (id)init
  21. {
  22. return [self initWithNumber:0];
  23. }
  24. - (id)initWithCoder:(NSCoder *)aDecoder
  25. {
  26. self = [super initWithCoder:aDecoder];
  27. if (self) {
  28. _number = 0;
  29. [self addNecessaryViews];
  30. [self setup];
  31. }
  32. return self;
  33. }
  34. - (id)initWithNumber:(NSInteger)number
  35. {
  36. self = [super initWithFrame:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)];
  37. if (self) {
  38. _number = 0;
  39. [self addNecessaryViews];
  40. [self setup];
  41. }
  42. return self;
  43. }
  44. // This method should be called only once
  45. - (void)addNecessaryViews
  46. {
  47. self.backgroundColor = kRoundedNumberViewDefaultBackgroundColor;
  48. self.numberLabel = [[UILabel alloc] init];
  49. self.numberLabel.font = [UIFont boldSystemFontOfSize:14];
  50. self.numberLabel.backgroundColor = [UIColor clearColor];
  51. _numberColor = kRoundedNumberViewDefaultTextColor;
  52. [self addSubview:self.numberLabel];
  53. }
  54. - (void)setup
  55. {
  56. NSInteger counter = _number;
  57. self.numberLabel.textColor = _numberColor;
  58. self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)counter];
  59. if (counter > kRoundedNumberViewCounterLimit) {
  60. self.numberLabel.text = [NSString stringWithFormat:@"%d+", kRoundedNumberViewCounterLimit];
  61. }
  62. [self.numberLabel sizeToFit];
  63. CGFloat frameWidth = self.numberLabel.frame.size.width + 16;
  64. CGFloat frameHeight = self.numberLabel.frame.size.height + self.numberLabel.frame.size.height / 2;
  65. self.frame = CGRectMake(0, 0, (frameWidth >= frameHeight) ? frameWidth : frameHeight, frameHeight);
  66. self.layer.cornerRadius = self.frame.size.height / 2;
  67. [self.numberLabel setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)];
  68. }
  69. - (void)setNumber:(NSInteger)number
  70. {
  71. if (_number != number) {
  72. _number = number;
  73. [self setup];
  74. }
  75. }
  76. - (void)setNumberColor:(UIColor *)numberColor
  77. {
  78. if (_numberColor != numberColor) {
  79. _numberColor = numberColor;
  80. self.numberLabel.textColor = _numberColor;
  81. }
  82. }
  83. - (void)setHighlightType:(HighlightType)highlightType
  84. {
  85. _highlightType = highlightType;
  86. self.layer.borderWidth = 0;
  87. switch (highlightType) {
  88. case kHighlightTypeNone:
  89. self.backgroundColor = [NCAppBranding placeholderColor];
  90. _numberColor = nil;
  91. break;
  92. case kHighlightTypeBorder:
  93. self.backgroundColor = [UIColor systemBackgroundColor];
  94. _numberColor = [NCAppBranding elementColor];
  95. self.layer.borderWidth = 2;
  96. self.layer.borderColor = [NCAppBranding elementColor].CGColor;
  97. break;
  98. case kHighlightTypeImportant:
  99. self.backgroundColor = [NCAppBranding themeColor];
  100. _numberColor = [NCAppBranding themeTextColor];
  101. break;
  102. }
  103. }
  104. @end