This commit is contained in:
2025-11-07 22:22:41 +08:00
parent b23c9a678b
commit 9a39c29e88
12 changed files with 731 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
//
// KBSearchBarView.h
// keyBoard
//
// 顶部搜索栏,独立封装的 View。
// - 左侧圆角输入框,右侧搜索按钮
// - 通过 block 将搜索事件回传给 VC
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBSearchBarView : UIView
/// 输入框
@property (nonatomic, strong, readonly) UITextField *textField;
/// 点击键盘 return 或右侧按钮时回调
@property (nonatomic, copy) void(^onSearch)(NSString *keyword);
/// 占位文字默认“Themes”
@property (nonatomic, copy) NSString *placeholder;
/// 外部可设置关键字
- (void)updateKeyword:(NSString *)keyword;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,124 @@
//
// KBSearchBarView.m
// keyBoard
//
//
//
#import "KBSearchBarView.h"
@interface KBSearchBarView () <UITextFieldDelegate>
@property (nonatomic, strong) UIView *bgView; //
@property (nonatomic, strong, readwrite) UITextField *textField; //
@property (nonatomic, strong) UIButton *searchButton; //
@end
@implementation KBSearchBarView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
}
return self;
}
- (void)setupUI {
//
[self addSubview:self.bgView];
[self.bgView addSubview:self.textField];
[self.bgView addSubview:self.searchButton];
// layout - Masonry
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
make.height.mas_equalTo(40);
}];
[self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bgView);
make.right.equalTo(self.bgView).offset(-8);
make.width.mas_equalTo(84);
make.height.mas_equalTo(32);
}];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.bgView).offset(12);
make.centerY.equalTo(self.bgView);
make.right.equalTo(self.searchButton.mas_left).offset(-8);
make.height.mas_equalTo(30);
}];
}
#pragma mark - Action
- (void)searchButtonTapped {
NSString *kw = self.textField.text ?: @"";
if (self.onSearch) { self.onSearch(kw); }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if (self.onSearch) { self.onSearch(textField.text ?: @""); }
return YES;
}
- (void)updateKeyword:(NSString *)keyword {
self.textField.text = keyword ?: @"";
}
#pragma mark - Lazy
- (UIView *)bgView {
if (!_bgView) {
_bgView = [[UIView alloc] init];
// +
_bgView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
_bgView.layer.cornerRadius = 20;
_bgView.layer.masksToBounds = YES;
}
return _bgView;
}
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_textField.delegate = self;
_textField.font = [UIFont systemFontOfSize:15];
_textField.textColor = [UIColor colorWithHex:0x1B1F1A];
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
_textField.returnKeyType = UIReturnKeySearch;
_textField.placeholder = @"Themes"; //
//
UIView *pad = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
_textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 1)];
_textField.leftViewMode = UITextFieldViewModeAlways;
(void)pad; // 使
}
return _textField;
}
- (UIButton *)searchButton {
if (!_searchButton) {
_searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
_searchButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
[_searchButton setTitle:@"Search" forState:UIControlStateNormal];
// 绿
[_searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_searchButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.72 blue:0.62 alpha:1.0];
_searchButton.layer.cornerRadius = 16;
_searchButton.layer.masksToBounds = YES;
[_searchButton addTarget:self action:@selector(searchButtonTapped) forControlEvents:UIControlEventTouchUpInside];
}
return _searchButton;
}
#pragma mark - Public
- (void)setPlaceholder:(NSString *)placeholder {
_placeholder = [placeholder copy];
self.textField.placeholder = placeholder;
}
@end

View File

@@ -0,0 +1,29 @@
//
// KBSearchSectionHeader.h
// keyBoard
//
// 通用区头:左侧标题,右侧可选垃圾桶按钮。
// 用于“历史搜索”和“推荐皮肤”两个 section。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBSearchSectionHeader : UICollectionReusableView
/// 标题
@property (nonatomic, strong, readonly) UILabel *titleLabel;
/// 右侧垃圾桶按钮(仅历史搜索使用)
@property (nonatomic, strong, readonly) UIButton *trashButton;
/// 配置标题与是否显示垃圾桶
- (void)configWithTitle:(NSString *)title showTrash:(BOOL)showTrash;
/// 清除按钮点击回调
@property (nonatomic, copy) void(^onTapTrash)(void);
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,72 @@
//
// KBSearchSectionHeader.m
// keyBoard
//
#import "KBSearchSectionHeader.h"
@interface KBSearchSectionHeader ()
@property (nonatomic, strong, readwrite) UILabel *titleLabel;
@property (nonatomic, strong, readwrite) UIButton *trashButton;
@end
@implementation KBSearchSectionHeader
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
}
return self;
}
- (void)setupUI {
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.titleLabel];
[self addSubview:self.trashButton];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(16);
make.centerY.equalTo(self);
make.right.lessThanOrEqualTo(self.trashButton.mas_left).offset(-8);
}];
[self.trashButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.right.equalTo(self).offset(-16);
make.width.height.mas_equalTo(24);
}];
}
- (void)tapTrash {
if (self.onTapTrash) { self.onTapTrash(); }
}
- (void)configWithTitle:(NSString *)title showTrash:(BOOL)showTrash {
self.titleLabel.text = title;
self.trashButton.hidden = !showTrash;
}
#pragma mark - Lazy
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _titleLabel;
}
- (UIButton *)trashButton {
if (!_trashButton) {
_trashButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_trashButton setImage:[UIImage systemImageNamed:@"trash"] forState:UIControlStateNormal];
_trashButton.tintColor = [UIColor colorWithHex:0x9A9A9A];
[_trashButton addTarget:self action:@selector(tapTrash) forControlEvents:UIControlEventTouchUpInside];
}
return _trashButton;
}
@end

View File

@@ -0,0 +1,17 @@
//
// KBSkinCardCell.h
// keyBoard
//
// 推荐皮肤 - 简单卡片 cell。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBSkinCardCell : UICollectionViewCell
- (void)configWithTitle:(NSString *)title imageURL:(nullable NSString *)url price:(NSString *)price;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,86 @@
//
// KBSkinCardCell.m
// keyBoard
//
#import "KBSkinCardCell.h"
@interface KBSkinCardCell ()
@property (nonatomic, strong) UIImageView *coverView; //
@property (nonatomic, strong) UILabel *titleLabel; //
@property (nonatomic, strong) UILabel *priceLabel; //
@end
@implementation KBSkinCardCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.layer.cornerRadius = 12;
self.contentView.layer.masksToBounds = YES;
self.contentView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.06].CGColor;
self.contentView.layer.shadowOpacity = 1;
self.contentView.layer.shadowOffset = CGSizeMake(0, 2);
[self.contentView addSubview:self.coverView];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.priceLabel];
[self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.contentView);
make.height.equalTo(self.contentView.mas_width).multipliedBy(0.75); // 4:3
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(12);
make.right.equalTo(self.contentView).offset(-12);
make.top.equalTo(self.coverView.mas_bottom).offset(8);
}];
[self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.titleLabel);
make.bottom.equalTo(self.contentView).offset(-10);
}];
}
return self;
}
- (void)configWithTitle:(NSString *)title imageURL:(NSString *)url price:(NSString *)price {
self.titleLabel.text = title.length ? title : @"Dopamine";
self.priceLabel.text = price.length ? price : @"20";
//
self.coverView.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1.0];
}
#pragma mark - Lazy
- (UIImageView *)coverView {
if (!_coverView) {
_coverView = [[UIImageView alloc] init];
_coverView.contentMode = UIViewContentModeScaleAspectFill;
_coverView.clipsToBounds = YES;
_coverView.backgroundColor = [UIColor colorWithWhite:0.94 alpha:1.0];
}
return _coverView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _titleLabel;
}
- (UILabel *)priceLabel {
if (!_priceLabel) {
_priceLabel = [[UILabel alloc] init];
_priceLabel.font = [UIFont systemFontOfSize:14];
_priceLabel.textColor = [UIColor colorWithRed:0.15 green:0.72 blue:0.62 alpha:1.0];
}
return _priceLabel;
}
@end

View File

@@ -0,0 +1,19 @@
//
// KBTagCell.h
// keyBoard
//
// 历史搜索 - 标签样式 cell。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBTagCell : UICollectionViewCell
- (void)config:(NSString *)text;
/// 根据文案计算自适应宽度(外部布局用)
+ (CGSize)sizeForText:(NSString *)text;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,50 @@
//
// KBTagCell.m
// keyBoard
//
#import "KBTagCell.h"
@interface KBTagCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation KBTagCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.contentView.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];
self.contentView.layer.cornerRadius = 16;
self.contentView.layer.masksToBounds = YES;
[self.contentView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(6, 12, 6, 12));
}];
}
return self;
}
- (void)config:(NSString *)text {
self.titleLabel.text = text ?: @"";
}
+ (CGSize)sizeForText:(NSString *)text {
if (text.length == 0) { return CGSizeMake(40, 32); }
CGSize s = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
// 12 + 12 32
return CGSizeMake(ceil(s.width) + 24, 32);
}
#pragma mark - Lazy
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:14];
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _titleLabel;
}
@end