208 lines
6.9 KiB
Objective-C
208 lines
6.9 KiB
Objective-C
//
|
|
// XPMineMenuTableViewCell.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2021/9/16.
|
|
//
|
|
|
|
#import "XPMineMenuTableViewCell.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
#import <YYText/YYText.h>
|
|
///Tool
|
|
#import "ThemeColor.h"
|
|
#import "XPMacro.h"
|
|
///Model
|
|
#import "XPMineItemModel.h"
|
|
|
|
@interface XPMineMenuTableViewCell ()
|
|
///背景
|
|
@property (nonatomic,strong) UIView * backView;
|
|
///
|
|
@property (nonatomic,strong) UIStackView *stackView;
|
|
///显示图片
|
|
@property (nonatomic,strong) UIImageView *logoImageView;
|
|
///显示名字
|
|
@property (nonatomic,strong) UILabel *titleLabel;
|
|
///箭头
|
|
@property (nonatomic,strong) UIButton *arrowButton;
|
|
///分割线
|
|
@property (nonatomic,strong) UIView * partLineView;
|
|
//访客记录未读数量
|
|
@property (nonatomic, strong) UILabel *unReadLabel;
|
|
@end
|
|
|
|
@implementation XPMineMenuTableViewCell
|
|
|
|
|
|
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
|
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
self.backgroundColor = [UIColor clearColor];
|
|
[self.contentView addSubview:self.backView];
|
|
|
|
[self.backView addSubview:self.stackView];
|
|
|
|
[self.stackView addArrangedSubview:self.logoImageView];
|
|
[self.stackView addArrangedSubview:self.titleLabel];
|
|
[self.stackView addArrangedSubview:self.unReadLabel];
|
|
[self.stackView addArrangedSubview:self.arrowButton];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.bottom.mas_equalTo(self.contentView);
|
|
make.right.left.mas_equalTo(self.contentView).inset(15);
|
|
}];
|
|
|
|
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.bottom.mas_equalTo(self.backView);
|
|
make.right.left.mas_equalTo(self.backView).inset(15);
|
|
}];
|
|
|
|
[self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.size.mas_equalTo(CGSizeMake(24, 24));
|
|
}];
|
|
|
|
[self.unReadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.titleLabel.mas_right).mas_offset(5);
|
|
make.height.mas_equalTo(15);
|
|
make.centerY.mas_equalTo(self.logoImageView.mas_centerY);
|
|
make.width.mas_equalTo(15);
|
|
}];
|
|
}
|
|
|
|
- (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius
|
|
{
|
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, KScreenWidth - 30 , 53) byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
|
|
CAShapeLayer *maskLayer = [CAShapeLayer layer];
|
|
maskLayer.path = path.CGPath;
|
|
self.backView.layer.mask = maskLayer;
|
|
}
|
|
|
|
- (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)setItemModel:(XPMineItemModel *)itemModel {
|
|
_itemModel = itemModel;
|
|
if (_itemModel) {
|
|
self.logoImageView.image = [UIImage imageNamed:_itemModel.itemImageName];
|
|
self.titleLabel.text = _itemModel.itemName;
|
|
if (itemModel.cornerType) {
|
|
[self applyRoundCorners:itemModel.cornerType radius:10];
|
|
}
|
|
[self.titleLabel sizeToFit];
|
|
if (itemModel.unReadCount) {
|
|
self.unReadLabel.hidden = NO;
|
|
} else {
|
|
self.unReadLabel.hidden = YES;
|
|
}
|
|
|
|
self.unReadLabel.attributedText = [self attributedStringWithLineSpace:10 str:[NSString stringWithFormat:@"%zd", itemModel.unReadCount]];
|
|
CGFloat width = [self getWidthWithContent:[NSString stringWithFormat:@"%zd", itemModel.unReadCount] height:15 font:11];
|
|
if (itemModel.unReadCount < 10) {
|
|
width = 7;
|
|
}
|
|
[self.unReadLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.titleLabel.mas_right).mas_offset(5);
|
|
make.height.mas_equalTo(15);
|
|
make.centerY.mas_equalTo(self.logoImageView.mas_centerY);
|
|
make.width.mas_equalTo(width + 8);
|
|
}];
|
|
}
|
|
}
|
|
|
|
- (UIView *)backView {
|
|
if (!_backView) {
|
|
_backView = [[UIView alloc] init];
|
|
_backView.backgroundColor = [ThemeColor appCellBackgroundColor];;
|
|
}
|
|
return _backView;
|
|
}
|
|
|
|
- (UIStackView *)stackView {
|
|
if (!_stackView) {
|
|
_stackView = [[UIStackView alloc] init];
|
|
_stackView.axis = UILayoutConstraintAxisHorizontal;
|
|
_stackView.distribution = UIStackViewDistributionFill;
|
|
_stackView.alignment = UIStackViewAlignmentCenter;
|
|
_stackView.spacing = 10;
|
|
_stackView.backgroundColor = [ThemeColor appCellBackgroundColor];
|
|
}
|
|
return _stackView;
|
|
}
|
|
|
|
- (UIImageView *)logoImageView {
|
|
if (!_logoImageView) {
|
|
_logoImageView = [[UIImageView alloc] init];
|
|
_logoImageView.userInteractionEnabled = YES;
|
|
}
|
|
return _logoImageView;
|
|
}
|
|
|
|
- (UILabel *)titleLabel {
|
|
if (!_titleLabel) {
|
|
_titleLabel = [[UILabel alloc] init];
|
|
_titleLabel.font = [UIFont systemFontOfSize:13];
|
|
_titleLabel.textColor = [ThemeColor secondTextColor];
|
|
}
|
|
return _titleLabel;
|
|
}
|
|
|
|
- (UIButton *)arrowButton {
|
|
if (!_arrowButton) {
|
|
_arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_arrowButton setImage:[UIImage imageNamed:@"mine_item_arrow"] forState:UIControlStateNormal];
|
|
[_arrowButton setImage:[UIImage imageNamed:@"mine_item_arrow"] forState:UIControlStateSelected];
|
|
[_arrowButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
|
|
}
|
|
return _arrowButton;
|
|
}
|
|
|
|
- (UIView *)partLineView {
|
|
if (!_partLineView) {
|
|
_partLineView = [[UIView alloc] init];
|
|
_partLineView.backgroundColor = [ThemeColor dividerColor];
|
|
}
|
|
return _partLineView;
|
|
}
|
|
|
|
- (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];
|
|
}
|
|
return _unReadLabel ;
|
|
}
|
|
|
|
@end
|