Files
yinmeng-ios/xplan-ios/Main/Mine/View/Cell/XPMineMenuTableViewCell.m

208 lines
6.9 KiB
Mathematica
Raw Normal View History

2021-09-16 19:30:22 +08:00
//
// XPMineMenuTableViewCell.m
// xplan-ios
//
// Created by on 2021/9/16.
//
#import "XPMineMenuTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
2022-01-26 15:51:19 +08:00
#import <YYText/YYText.h>
2021-09-16 19:30:22 +08:00
///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;
2022-01-26 15:51:19 +08:00
///线
@property (nonatomic,strong) UIView * partLineView;
//访
@property (nonatomic, strong) UILabel *unReadLabel;
2021-09-16 19:30:22 +08:00
@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];
2022-01-26 15:51:19 +08:00
[self.stackView addArrangedSubview:self.unReadLabel];
2021-09-16 19:30:22 +08:00
[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));
}];
2022-01-26 15:51:19 +08:00
[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);
}];
2021-09-16 19:30:22 +08:00
}
- (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;
}
2022-01-26 15:51:19 +08:00
- (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;
}
2021-09-16 19:30:22 +08:00
#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;
2021-09-16 19:30:22 +08:00
if (itemModel.cornerType) {
[self applyRoundCorners:itemModel.cornerType radius:10];
}
2022-01-26 15:51:19 +08:00
[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);
}];
2021-09-16 19:30:22 +08:00
}
}
- (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;
}
2022-01-26 15:51:19 +08:00
- (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 ;
}
2021-09-16 19:30:22 +08:00
@end