EaseImageView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseImageView.h"
  13. @interface EaseImageView()
  14. @property (strong, nonatomic) UILabel *badgeView;
  15. @property (nonatomic) NSLayoutConstraint *badgeWidthConstraint;
  16. @end
  17. @implementation EaseImageView
  18. + (void)initialize
  19. {
  20. // UIAppearance Proxy Defaults
  21. EaseImageView *imageView = [self appearance];
  22. imageView.badgeBackgroudColor = [UIColor redColor];
  23. imageView.badgeTextColor = [UIColor whiteColor];
  24. imageView.badgeFont = [UIFont boldSystemFontOfSize:11];
  25. imageView.imageCornerRadius = 0;
  26. imageView.badgeSize = 20;
  27. }
  28. - (instancetype)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. [self _setupSubviews];
  33. }
  34. return self;
  35. }
  36. - (instancetype)initWithFrame:(CGRect)frame
  37. {
  38. self = [super initWithFrame:frame];
  39. if (self) {
  40. [self _setupSubviews];
  41. }
  42. return self;
  43. }
  44. - (id)initWithCoder:(NSCoder *)aDecoder
  45. {
  46. self = [super initWithCoder:aDecoder];
  47. if (self) {
  48. [self _setupSubviews];
  49. }
  50. return self;
  51. }
  52. #pragma mark - private
  53. - (void)_setupSubviews
  54. {
  55. if (_imageView == nil) {
  56. self.clipsToBounds = NO;
  57. self.backgroundColor = [UIColor clearColor];
  58. _imageView = [[UIImageView alloc] init];
  59. _imageView.translatesAutoresizingMaskIntoConstraints = NO;
  60. _imageView.layer.cornerRadius = _imageCornerRadius;
  61. _imageView.clipsToBounds = YES;
  62. _imageView.backgroundColor = [UIColor grayColor];
  63. [self addSubview:_imageView];
  64. _badgeView = [[UILabel alloc] init];
  65. _badgeView.translatesAutoresizingMaskIntoConstraints = NO;
  66. _badgeView.textAlignment = NSTextAlignmentCenter;
  67. _badgeView.textColor = _badgeTextColor;
  68. _badgeView.backgroundColor = _badgeBackgroudColor;
  69. _badgeView.font = _badgeFont;
  70. _badgeView.hidden = YES;
  71. _badgeView.layer.cornerRadius = _badgeSize / 2;
  72. _badgeView.clipsToBounds = YES;
  73. [self addSubview:_badgeView];
  74. [self _setupImageViewConstraint];
  75. [self _setupBadgeViewConstraint];
  76. }
  77. }
  78. #pragma mark - Setup Constraint
  79. /*!
  80. @method
  81. @brief 设置头像约束
  82. @discussion
  83. @result
  84. */
  85. - (void)_setupImageViewConstraint
  86. {
  87. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
  88. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
  89. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  90. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  91. }
  92. /*!
  93. @method
  94. @brief 设置角标约束
  95. @discussion
  96. @result
  97. */
  98. - (void)_setupBadgeViewConstraint
  99. {
  100. self.badgeWidthConstraint = [NSLayoutConstraint constraintWithItem:self.badgeView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:self.badgeSize];
  101. [self addConstraint:self.badgeWidthConstraint];
  102. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.badgeView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.badgeView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
  103. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.badgeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.imageCornerRadius + 3]];
  104. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.badgeView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-3]];
  105. }
  106. - (void)_updateBadgeViewWidthConstraint
  107. {
  108. [self removeConstraint:self.badgeWidthConstraint];
  109. self.badgeWidthConstraint = [NSLayoutConstraint constraintWithItem:self.badgeView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:self.badgeSize];
  110. [self addConstraint:self.badgeWidthConstraint];
  111. }
  112. #pragma mark - setter
  113. - (void)setImage:(UIImage *)image
  114. {
  115. _image = image;
  116. self.imageView.image = image;
  117. }
  118. - (void)setBadge:(NSInteger)badge
  119. {
  120. _badge = badge;
  121. if (badge > 0) {
  122. self.badgeView.hidden = NO;
  123. }
  124. else{
  125. self.badgeView.hidden = YES;
  126. }
  127. if (badge > 99) {
  128. self.badgeView.text = @"N+";
  129. }
  130. else{
  131. self.badgeView.text = [NSString stringWithFormat:@"%ld", (long)_badge];
  132. }
  133. }
  134. - (void)setShowBadge:(BOOL)showBadge
  135. {
  136. if (_showBadge != showBadge) {
  137. _showBadge = showBadge;
  138. self.badgeView.hidden = !_showBadge;
  139. }
  140. }
  141. - (void)setBadgeSize:(CGFloat)badgeSize
  142. {
  143. if (_badgeSize != badgeSize) {
  144. _badgeSize = badgeSize;
  145. _badgeView.layer.cornerRadius = _badgeSize / 2;
  146. [self _updateBadgeViewWidthConstraint];
  147. }
  148. }
  149. - (void)setImageCornerRadius:(CGFloat)imageCornerRadius
  150. {
  151. if (_imageCornerRadius != imageCornerRadius) {
  152. _imageCornerRadius = imageCornerRadius;
  153. self.imageView.layer.cornerRadius = _imageCornerRadius;
  154. }
  155. }
  156. - (void)setBadgeFont:(UIFont *)badgeFont
  157. {
  158. _badgeFont = badgeFont;
  159. self.badgeView.font = badgeFont;
  160. }
  161. - (void)setBadgeTextColor:(UIColor *)badgeTextColor
  162. {
  163. _badgeTextColor = badgeTextColor;
  164. self.badgeView.textColor = badgeTextColor;
  165. }
  166. - (void)setBadgeBackgroudColor:(UIColor *)badgeBackgroudColor
  167. {
  168. _badgeBackgroudColor = badgeBackgroudColor;
  169. self.badgeView.backgroundColor = badgeBackgroudColor;
  170. }
  171. @end