Files
keyboard/keyBoard/Class/Me/V/KBMyKeyboardCell.m

188 lines
6.6 KiB
Mathematica
Raw Normal View History

2025-11-10 20:40:11 +08:00
//
// KBMyKeyboardCell.m
// keyBoard
//
#import "KBMyKeyboardCell.h"
@interface KBMyKeyboardCell ()
@property (nonatomic, strong) UIView *coverView; //
@property (nonatomic, strong) UILabel *emojiLabel; //
@property (nonatomic, strong) UILabel *titleLabel; //
@property (nonatomic, strong) UIView *minusBadge; //
@property (nonatomic, strong) UILabel *minusLabel; //
2025-11-11 20:24:13 +08:00
@property (nonatomic, strong) UIControl *minusTapArea; //
2025-11-10 20:40:11 +08:00
@end
@implementation KBMyKeyboardCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.contentView.backgroundColor = [UIColor clearColor];
//
self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.08].CGColor;
self.layer.shadowOpacity = 1.0;
self.layer.shadowOffset = CGSizeMake(0, 2);
self.layer.shadowRadius = 6;
[self.contentView addSubview:self.coverView];
[self.coverView addSubview:self.emojiLabel];
[self.coverView addSubview:self.titleLabel];
[self.contentView addSubview:self.minusBadge];
[self.minusBadge addSubview:self.minusLabel];
[self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.left.equalTo(self.contentView).offset(0);
make.height.mas_equalTo(36);
}];
// Masonry 12
[self.emojiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.coverView).offset(12);
make.centerY.equalTo(self.coverView);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.emojiLabel.mas_right).offset(8);
make.centerY.equalTo(self.coverView);
make.right.lessThanOrEqualTo(self.coverView).offset(-12);
}];
// hidden
[self.minusBadge mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(11);
make.top.equalTo(self.contentView).offset(0);
make.right.equalTo(self.contentView).offset(0);
}];
[self.minusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.minusBadge);
}];
2025-11-10 21:33:00 +08:00
2025-11-11 20:24:13 +08:00
//
[self.contentView addSubview:self.minusTapArea];
[self.minusTapArea mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(0);
make.right.equalTo(self.contentView).offset(0);
make.width.height.mas_equalTo(32); // 32x32
}];
// minusBadge
[self.minusTapArea addTarget:self action:@selector(didTapMinus) forControlEvents:UIControlEventTouchUpInside];
2025-11-10 21:33:00 +08:00
self.minusBadge.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMinus)];
[self.minusBadge addGestureRecognizer:tap];
2025-11-10 20:40:11 +08:00
}
return self;
}
- (void)prepareForReuse {
[super prepareForReuse];
self.emojiLabel.text = @"";
self.titleLabel.text = @"";
}
- (void)configEmoji:(NSString *)emoji title:(NSString *)title {
// UI
self.emojiLabel.text = emoji ?: @"";
self.titleLabel.text = title ?: @"";
}
+ (CGSize)sizeForEmoji:(NSString *)emoji title:(NSString *)title {
// 44 = (12+12) + Emoji ( 20 ) + (8) +
UIFont *emojiFont = [UIFont systemFontOfSize:20];
UIFont *titleFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
CGFloat emojiW = 0;
if (emoji.length > 0) {
CGSize s = [emoji sizeWithAttributes:@{NSFontAttributeName:emojiFont}];
emojiW = ceil(s.width);
} else {
emojiW = 0; //
}
CGFloat textW = 0;
if (title.length > 0) {
CGSize s = [title sizeWithAttributes:@{NSFontAttributeName:titleFont}];
textW = ceil(s.width);
}
CGFloat width = 12 + emojiW + (emojiW > 0 ? 8 : 0) + textW + 12;
CGFloat minW = 84; //
CGFloat maxW = UIScreen.mainScreen.bounds.size.width - 16 * 2; //
width = MAX(minW, MIN(width, maxW));
return CGSizeMake(width, 41.0);
}
#pragma mark - Lazy
- (UILabel *)emojiLabel {
if (!_emojiLabel) {
_emojiLabel = [UILabel new];
_emojiLabel.font = [UIFont systemFontOfSize:20];
_emojiLabel.text = @"😀"; //
}
return _emojiLabel;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel new];
_titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _titleLabel;
}
- (UIView *)minusBadge {
if (!_minusBadge) {
_minusBadge = [UIView new];
_minusBadge.backgroundColor = [UIColor colorWithHex:KBColorValue];
_minusBadge.layer.cornerRadius = 5.5;
_minusBadge.layer.masksToBounds = YES;
}
return _minusBadge;
}
- (UILabel *)minusLabel {
if (!_minusLabel) {
_minusLabel = [UILabel new];
_minusLabel.text = @"-";
_minusLabel.textColor = [UIColor whiteColor];
_minusLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
_minusLabel.textAlignment = NSTextAlignmentCenter;
}
return _minusLabel;
}
2025-11-11 20:24:13 +08:00
- (UIControl *)minusTapArea {
if (!_minusTapArea) {
_minusTapArea = [[UIControl alloc] init];
_minusTapArea.backgroundColor = [UIColor clearColor];
// 使
_minusTapArea.accessibilityLabel = @"删除";
}
return _minusTapArea;
}
2025-11-10 20:40:11 +08:00
- (UIView *)coverView{
if (!_coverView) {
_coverView = [[UIView alloc] init];
_coverView.backgroundColor = [UIColor whiteColor];
_coverView.layer.cornerRadius = 4;
_coverView.layer.masksToBounds = true;
}
return _coverView;
}
2025-11-10 21:33:00 +08:00
#pragma mark - Actions
// VC
- (void)didTapMinus {
if (self.onMinusTapped) {
self.onMinusTapped(self);
}
}
2025-11-10 20:40:11 +08:00
2025-11-10 21:33:00 +08:00
@end