This commit is contained in:
2025-11-11 14:56:57 +08:00
parent 57bd4ba109
commit 17b8bf2bfd
6 changed files with 556 additions and 1 deletions

View File

@@ -0,0 +1,250 @@
//
// KBPersonInfoItemCell.m
// keyBoard
//
// Created by Codex on 2025/11/11.
//
@import UIKit;
#import "KBPersonInfoItemCell.h"
#import <Masonry/Masonry.h>
@interface KBPersonInfoItemCell ()
//
@property (nonatomic, strong) UIView *cardView;
//
@property (nonatomic, strong) UILabel *titleLabel;
//
@property (nonatomic, strong) UILabel *valueLabel;
// >
@property (nonatomic, strong) UIImageView *arrowView;
//
@property (nonatomic, strong) UIButton *copyBtn;
// 线
@property (nonatomic, strong) UIView *line;
// layoutSubviews mask
@property (nonatomic, assign) UIRectCorner cornerRecord;
@end
@implementation KBPersonInfoItemCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.backgroundColor = UIColor.clearColor;
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.cardView];
[self.cardView addSubview:self.titleLabel];
[self.cardView addSubview:self.valueLabel];
[self.cardView addSubview:self.arrowView];
[self.cardView addSubview:self.copyBtn];
[self.cardView addSubview:self.line];
[self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(16);
make.right.equalTo(self.contentView).offset(-16);
make.top.equalTo(self.contentView);
make.bottom.equalTo(self.contentView);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.cardView).offset(16);
make.centerY.equalTo(self.cardView);
}];
[self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.cardView).offset(-16);
make.centerY.equalTo(self.cardView);
make.width.mas_equalTo(10);
make.height.mas_equalTo(16);
}];
[self.copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.cardView);
make.right.equalTo(self.cardView).offset(-16);
make.width.height.mas_equalTo(22);
}];
[self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.cardView);
make.right.equalTo(self.arrowView.mas_left).offset(-8);
}];
[self.line mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.cardView).offset(16);
make.right.equalTo(self.cardView).offset(-16);
make.bottom.equalTo(self.cardView);
make.height.mas_equalTo(0.5);
}];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
//
self.cardView.layer.cornerRadius = 12.0;
// corner mask
if (self.cornerRecord != 0 && !CGRectIsEmpty(self.cardView.bounds)) {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.cardView.bounds
byRoundingCorners:self.cornerRecord
cornerRadii:CGSizeMake(12, 12)];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.frame = self.cardView.bounds; mask.path = path.CGPath;
self.cardView.layer.mask = mask;
} else {
self.cardView.layer.mask = nil;
}
}
- (void)prepareForReuse {
[super prepareForReuse];
self.arrowView.hidden = YES; self.copyBtn.hidden = YES;
self.line.hidden = NO;
}
- (void)configWithTitle:(NSString *)title
value:(NSString *)value
showArrow:(BOOL)showArrow
showCopy:(BOOL)showCopy
isTop:(BOOL)isTop
isBottom:(BOOL)isBottom {
self.titleLabel.text = title ?: @"";
self.valueLabel.text = value ?: @"";
self.arrowView.hidden = !showArrow;
self.copyBtn.hidden = !showCopy;
self.valueLabel.textColor = showArrow ? [UIColor blackColor] : [UIColor colorWithWhite:0.2 alpha:1.0];
//
UIView *rightAnchor = showCopy ? self.copyBtn : self.arrowView;
[self.valueLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.cardView);
make.right.equalTo(rightAnchor.mas_left).offset(-8);
}];
// / + 线
UIRectCorner corners = 0;
if (isTop && isBottom) {
corners = UIRectCornerAllCorners;
self.line.hidden = YES;
} else if (isTop) {
corners = UIRectCornerTopLeft | UIRectCornerTopRight;
self.line.hidden = NO;
} else if (isBottom) {
corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
self.line.hidden = YES; // 线
} else {
corners = 0; self.line.hidden = NO;
}
// 使
// layoutSubviews mask
self.cornerRecord = corners;
[self setNeedsLayout];
}
#pragma mark - Lazy UI
- (UIView *)cardView {
if (!_cardView) {
_cardView = [UIView new];
_cardView.backgroundColor = UIColor.whiteColor;
_cardView.layer.cornerRadius = 12.0;
_cardView.layer.masksToBounds = YES; //
}
return _cardView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel new];
_titleLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
_titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
}
return _titleLabel;
}
- (UILabel *)valueLabel {
if (!_valueLabel) {
_valueLabel = [UILabel new];
_valueLabel.textColor = [UIColor blackColor];
_valueLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
_valueLabel.textAlignment = NSTextAlignmentRight;
[_valueLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return _valueLabel;
}
- (UIImageView *)arrowView {
if (!_arrowView) {
_arrowView = [[UIImageView alloc] init];
_arrowView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *img = nil;
if (@available(iOS 13.0, *)) {
img = [UIImage systemImageNamed:@"chevron.right"];
}
if (!img) {
//
CGSize size = CGSizeMake(10, 16);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
UIBezierPath *p = [UIBezierPath bezierPath];
[p moveToPoint:CGPointMake(2, 2)];
[p addLineToPoint:CGPointMake(8, 8)];
[p addLineToPoint:CGPointMake(2, 14)];
[[UIColor colorWithWhite:0.7 alpha:1] setStroke];
p.lineWidth = 2; [p stroke];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
_arrowView.image = img;
_arrowView.tintColor = [UIColor colorWithWhite:0.7 alpha:1];
_arrowView.hidden = YES;
}
return _arrowView;
}
- (UIButton *)copyBtn {
if (!_copyBtn) {
_copyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
UIImage *img = nil;
if (@available(iOS 13.0, *)) {
img = [UIImage systemImageNamed:@"doc.on.doc"];
}
if (!img) {
// copy
CGSize sz = CGSizeMake(22, 22);
UIGraphicsBeginImageContextWithOptions(sz, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor colorWithWhite:0.85 alpha:1] setStroke];
CGContextSetLineWidth(ctx, 2);
CGContextStrokeRect(ctx, CGRectMake(6, 6, 12, 12));
CGContextStrokeRect(ctx, CGRectMake(4, 4, 12, 12));
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
[_copyBtn setImage:img forState:UIControlStateNormal];
_copyBtn.tintColor = [UIColor colorWithWhite:0.6 alpha:1];
_copyBtn.hidden = YES;
[_copyBtn addTarget:self action:@selector(onCopy) forControlEvents:UIControlEventTouchUpInside];
}
return _copyBtn;
}
- (UIView *)line {
if (!_line) {
_line = [UIView new];
_line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
}
return _line;
}
#pragma mark - Event
- (void)onCopy {
// valueLabel
NSString *txt = self.valueLabel.text ?: @"";
if (txt.length == 0) return;
UIPasteboard.generalPasteboard.string = txt;
}
@end