152 lines
4.7 KiB
Objective-C
152 lines
4.7 KiB
Objective-C
//
|
|
// YMMineFriendNumberView.m
|
|
// YUMI
|
|
//
|
|
// Created by YUMI on 2021/12/21.
|
|
//
|
|
|
|
#import "XPMineFriendNumberView.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
///Tool
|
|
#import "DJDKMIMOMColor.h"
|
|
|
|
|
|
@interface XPMineFriendNumberView ()
|
|
///显示个数
|
|
@property (nonatomic,strong) UILabel *numberLabel;
|
|
///显示标题
|
|
@property (nonatomic,strong) UILabel *titleLabel;
|
|
//访客记录未读数量
|
|
@property (nonatomic, strong) UILabel *unReadLabel;
|
|
|
|
@end
|
|
|
|
@implementation XPMineFriendNumberView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
[self addSubview:self.numberLabel];
|
|
[self addSubview:self.titleLabel];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.mas_equalTo(self.titleLabel.mas_bottom);
|
|
}];
|
|
|
|
[self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self);
|
|
make.centerX.mas_equalTo(self);
|
|
make.height.mas_equalTo(24);
|
|
}];
|
|
|
|
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo(self.numberLabel.mas_bottom).offset(4);
|
|
make.left.right.mas_equalTo(self);
|
|
}];
|
|
}
|
|
|
|
- (NSAttributedString *)attributedStringWithLineSpace:(CGFloat)lineSpace str:(NSString *)str{
|
|
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
|
|
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xffffff), NSFontAttributeName: [UIFont systemFontOfSize:11]}];
|
|
[attributedString appendAttributedString:title];
|
|
attributedString.yy_alignment = NSTextAlignmentCenter;
|
|
return attributedString;
|
|
}
|
|
|
|
- (CGFloat)getWidthWithContent:(NSString *)content height:(CGFloat)height font:(CGFloat)font{
|
|
|
|
CGRect rect = [content boundingRectWithSize:CGSizeMake(999, height)
|
|
options:NSStringDrawingUsesLineFragmentOrigin
|
|
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
|
|
context:nil];
|
|
return rect.size.width;
|
|
}
|
|
|
|
#pragma mark - Getters And Setters
|
|
- (void)setTitle:(NSString *)title {
|
|
self.titleLabel.text = title;
|
|
}
|
|
|
|
- (void)setNumber:(NSString *)number {
|
|
self.numberLabel.text = number;
|
|
}
|
|
|
|
- (void)setIsVisitor:(BOOL)isVisitor {
|
|
if (isVisitor) {
|
|
[self addSubview:self.unReadLabel];
|
|
[self.unReadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.numberLabel.mas_right).mas_offset(5);
|
|
make.height.mas_equalTo(15);
|
|
make.width.mas_equalTo(15);
|
|
make.top.mas_equalTo(self.numberLabel);
|
|
}];
|
|
}
|
|
}
|
|
|
|
- (void)setVisitorNum:(NSInteger)visitorNum {
|
|
if (visitorNum > 0) {
|
|
self.unReadLabel.hidden = NO;
|
|
self.unReadLabel.attributedText = [self attributedStringWithLineSpace:10 str:[NSString stringWithFormat:@"%zd", visitorNum]];
|
|
CGFloat width = [self getWidthWithContent:[NSString stringWithFormat:@"%zd", visitorNum] height:15 font:11];
|
|
if (visitorNum < 10) {
|
|
width = 7;
|
|
}
|
|
[self.unReadLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.numberLabel.mas_right).mas_offset(5);
|
|
make.width.mas_equalTo(width + 8);
|
|
make.top.mas_equalTo(self.numberLabel);
|
|
make.height.mas_equalTo(15);
|
|
}];
|
|
} else {
|
|
self.unReadLabel.hidden = YES;
|
|
}
|
|
}
|
|
|
|
- (UILabel *)numberLabel {
|
|
if (!_numberLabel) {
|
|
_numberLabel = [[UILabel alloc] init];
|
|
_numberLabel.font = [UIFont boldSystemFontOfSize:20];
|
|
_numberLabel.textAlignment = NSTextAlignmentCenter;
|
|
_numberLabel.textColor = [DJDKMIMOMColor mainTextColor];
|
|
}
|
|
return _numberLabel;
|
|
}
|
|
|
|
- (UILabel *)titleLabel {
|
|
if (!_titleLabel) {
|
|
_titleLabel = [[UILabel alloc] init];
|
|
_titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
|
|
_titleLabel.textColor = [DJDKMIMOMColor textThirdColor];
|
|
_titleLabel.textAlignment = NSTextAlignmentCenter;
|
|
}
|
|
return _titleLabel;
|
|
}
|
|
|
|
- (UILabel *)unReadLabel {
|
|
if (!_unReadLabel) {
|
|
_unReadLabel = [[UILabel alloc] init];
|
|
_unReadLabel.font = [UIFont systemFontOfSize:11];
|
|
_unReadLabel.textColor = [UIColor whiteColor];
|
|
_unReadLabel.backgroundColor = UIColorFromRGB(0xFF5B55);
|
|
_unReadLabel.layer.cornerRadius = 7.5;
|
|
_unReadLabel.layer.masksToBounds = YES;
|
|
_unReadLabel.textAlignment = NSTextAlignmentCenter;
|
|
[_unReadLabel sizeToFit];
|
|
_unReadLabel.hidden = YES;
|
|
}
|
|
return _unReadLabel ;
|
|
}
|
|
|
|
@end
|