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