Files
keyboard/keyBoard/Class/Me/V/KBMyKeyboardCell.m
2025-11-10 20:40:11 +08:00

162 lines
5.4 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// KBMyKeyboardCell.m
// keyBoard
//
// 截图页标签样式 cell实现
// - 左侧 Emoji中间标题
// - 右上角小圆点(减号),用于删除编辑态展示;
// - 白底圆角,阴影/描边轻微以贴近设计;
// - 暴露 +sizeForEmoji:title: 以便外部根据文本动态决定 item 宽度。
//
#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; // 减号
@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);
}];
}
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;
}
- (UIView *)coverView{
if (!_coverView) {
_coverView = [[UIView alloc] init];
_coverView.backgroundColor = [UIColor whiteColor];
_coverView.layer.cornerRadius = 4;
_coverView.layer.masksToBounds = true;
}
return _coverView;
}
@end