This commit is contained in:
2025-11-08 20:04:50 +08:00
parent faeb930fe3
commit 3b0beb52da
8 changed files with 620 additions and 17 deletions

View File

@@ -0,0 +1,18 @@
//
// KBHistoryMoreCell.h
// keyBoard
//
// 历史记录第二行尾部的“展开更多”按钮 cell向下箭头
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBHistoryMoreCell : UICollectionViewCell
/// 固定尺寸建议(高度与标签一致 32
+ (CGSize)fixedSize;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,56 @@
//
// KBHistoryMoreCell.m
// keyBoard
//
#import "KBHistoryMoreCell.h"
@interface KBHistoryMoreCell ()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *iconView;
@end
@implementation KBHistoryMoreCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.contentView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.bgView];
[self.bgView addSubview:self.iconView];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView);
}];
[self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.bgView);
make.width.height.mas_equalTo(14);
}];
}
return self;
}
+ (CGSize)fixedSize { return CGSizeMake(32, 32); }
#pragma mark - Lazy
- (UIView *)bgView {
if (!_bgView) {
_bgView = [[UIView alloc] init];
_bgView.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];
_bgView.layer.cornerRadius = 16;
_bgView.layer.masksToBounds = YES;
}
return _bgView;
}
- (UIImageView *)iconView {
if (!_iconView) {
_iconView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"chevron.down"]];
_iconView.tintColor = [UIColor colorWithHex:0x9A9A9A];
_iconView.contentMode = UIViewContentModeScaleAspectFit;
}
return _iconView;
}
@end

View File

@@ -0,0 +1,21 @@
//
// KBSearchResultVC.h
// keyBoard
//
// 搜索结果页:顶部搜索框 + 下方网格列表
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
/// 简单的搜索结果页,供外部 push 使用
@interface KBSearchResultVC : UIViewController
/// 可选:进入页面时默认关键字
@property (nonatomic, copy, nullable) NSString *defaultKeyword;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,169 @@
//
// KBSearchResultVC.m
// keyBoard
//
// KBSearchBarView UICollectionView
// - Masonry
// -
// -
//
#import "KBSearchResultVC.h"
#import "KBSearchBarView.h"
#import "KBSkinCardCell.h"
static NSString * const kResultCellId = @"KBSkinCardCell";
@interface KBSearchResultVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
// KBSearchBarView
@property (nonatomic, strong) KBSearchBarView *searchBarView;
//
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
//
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *resultItems; // @{title, price}
@end
@implementation KBSearchResultVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//
[self.view addSubview:self.searchBarView];
[self.view addSubview:self.collectionView];
// Masonry
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
// 16 40
make.top.equalTo(self.view.mas_top).offset(KB_NAV_TOTAL_HEIGHT + 8);
make.left.equalTo(self.view).offset(16);
make.right.equalTo(self.view).offset(-16);
make.height.mas_equalTo(40);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.searchBarView.mas_bottom).offset(12);
make.left.right.bottom.equalTo(self.view);
}];
//
if (self.defaultKeyword.length > 0) {
[self.searchBarView updateKeyword:self.defaultKeyword];
[self performSearch:self.defaultKeyword];
} else {
//
[self loadMockData];
}
}
#pragma mark - Private
///
- (void)performSearch:(NSString *)keyword {
// 10
[self.resultItems removeAllObjects];
for (int i = 0; i < 10; i++) {
[self.resultItems addObject:@{ @"title": @"Dopamine", @"price": @"20" }];
}
[self.collectionView reloadData];
}
///
- (void)loadMockData {
[self.resultItems removeAllObjects];
for (int i = 0; i < 12; i++) {
[self.resultItems addObject:@{ @"title": @"Dopamine", @"price": @"20" }];
}
[self.collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.resultItems.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kResultCellId forIndexPath:indexPath];
NSDictionary *it = self.resultItems[indexPath.item];
[cell configWithTitle:it[@"title"] imageURL:nil price:it[@"price"]];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//
CGFloat width = collectionView.bounds.size.width;
CGFloat inset = 16; //
CGFloat spacing = 12; //
CGFloat w = floor((width - inset * 2 - spacing) / 2.0);
CGFloat h = w * 0.75 + 8 + 20 + 10 + 6 + 8; // KBSkinCardCell
return CGSizeMake(w, h);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(8, 16, 20, 16);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 12;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 16;
}
#pragma mark - Lazy
- (KBSearchBarView *)searchBarView {
if (!_searchBarView) {
_searchBarView = [[KBSearchBarView alloc] init];
_searchBarView.placeholder = @"Themes";
__weak typeof(self) weakSelf = self;
_searchBarView.onSearch = ^(NSString * _Nonnull keyword) {
[weakSelf performSearch:keyword];
};
}
return _searchBarView;
}
- (UICollectionViewFlowLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return _flowLayout;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
// cell
[_collectionView registerClass:KBSkinCardCell.class forCellWithReuseIdentifier:kResultCellId];
}
return _collectionView;
}
- (NSMutableArray<NSDictionary *> *)resultItems {
if (!_resultItems) {
_resultItems = [NSMutableArray array];
}
return _resultItems;
}
@end

View File

@@ -10,10 +10,16 @@
#import "KBSearchSectionHeader.h"
#import "KBTagCell.h"
#import "KBSkinCardCell.h"
#import "KBHistoryMoreCell.h"
#import "KBSearchResultVC.h"
#import "UICollectionViewLeftAlignedLayout.h"
static NSString * const kTagCellId = @"KBTagCell";
static NSString * const kTagCellId = @"KBTagCell";
static NSString * const kSkinCellId = @"KBSkinCardCell";
static NSString * const kHeaderId = @"KBSearchSectionHeader";
static NSString * const kHeaderId = @"KBSearchSectionHeader";
static NSString * const kMoreCellId = @"KBHistoryMoreCell";
static NSString * const kMoreToken = @"__KB_MORE__"; //
typedef NS_ENUM(NSInteger, KBSearchSection) {
KBSearchSectionHistory = 0,
@@ -26,11 +32,13 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
@property (nonatomic, strong) UIView *titleContainer; // searchBarView
//
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UICollectionViewLeftAlignedLayout *flowLayout;
//
@property (nonatomic, strong) NSMutableArray<NSString *> *historyWords; //
@property (nonatomic, strong) NSArray<NSDictionary *> *recommendItems; // title/price
@property (nonatomic, assign) BOOL historyExpanded; //
@property (nonatomic, assign) CGFloat lastCollectionWidth; //
@end
@implementation KBSearchVC
@@ -65,18 +73,137 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
[self.collectionView reloadData];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat w = self.collectionView.bounds.size.width;
if (w > 0 && fabs(w - self.lastCollectionWidth) > 0.5) {
self.lastCollectionWidth = w;
[self.collectionView reloadData];
}
}
#pragma mark - Private
///
/// /
- (void)performSearch:(NSString *)kw {
NSString *trim = [kw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trim.length == 0) { return; }
//
[self.historyWords removeObject:trim];
//
for (NSInteger i = (NSInteger)self.historyWords.count - 1; i >= 0; i--) {
NSString *old = self.historyWords[i];
if ([old caseInsensitiveCompare:trim] == NSOrderedSame) {
[self.historyWords removeObjectAtIndex:i];
}
}
//
[self.historyWords insertObject:trim atIndex:0];
[self.collectionView reloadData];
}
///
- (void)openResultForKeyword:(NSString *)kw {
NSString *trim = [kw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trim.length == 0) { return; }
KBSearchResultVC *vc = [[KBSearchResultVC alloc] init];
vc.defaultKeyword = trim;
[self.navigationController pushViewController:vc animated:YES];
}
///
- (NSArray<NSString *> *)currentDisplayHistory {
if (self.historyExpanded) { return self.historyWords; }
if (self.collectionView.bounds.size.width <= 0) { return self.historyWords; }
CGFloat width = self.collectionView.bounds.size.width;
UIEdgeInsets inset = UIEdgeInsetsMake(8, 16, 12, 16);
CGFloat available = width - inset.left - inset.right;
CGFloat itemSpacing = 8.0; //
NSMutableArray<NSString *> *display = [NSMutableArray array];
NSInteger line = 1;
CGFloat lineUsed = 0.0; //
// 2 item display 便退
NSMutableArray<NSNumber *> *lineWidths = [NSMutableArray array];
NSMutableArray<NSNumber *> *lineIndexes = [NSMutableArray array];
for (NSInteger i = 0; i < self.historyWords.count; i++) {
NSString *text = self.historyWords[i];
CGSize sz = [KBTagCell sizeForText:text];
CGFloat w = sz.width;
CGFloat need = (lineUsed == 0 ? w : (lineUsed + itemSpacing + w));
if (need <= available) {
//
[display addObject:text];
if (line == 2) {
[lineWidths addObject:@(w)];
[lineIndexes addObject:@(display.count - 1)];
}
lineUsed = need;
continue;
}
//
if (line == 1) {
// 2
line = 2;
lineUsed = 0.0;
[lineWidths removeAllObjects];
[lineIndexes removeAllObjects];
// 2
if (w <= available) {
[display addObject:text];
[lineWidths addObject:@(w)];
[lineIndexes addObject:@(display.count - 1)];
lineUsed = w;
continue;
} else {
//
[display addObject:kMoreToken];
return display;
}
}
// 2
CGSize moreSize = [KBHistoryMoreCell fixedSize];
CGFloat moreNeed = (lineUsed == 0 ? moreSize.width : (lineUsed + itemSpacing + moreSize.width));
if (moreNeed <= available) {
[display addObject:kMoreToken];
return display;
}
// 退
while (lineIndexes.count > 0) {
NSNumber *lastIndexNum = lineIndexes.lastObject; // display
NSNumber *lastWidthNum = lineWidths.lastObject;
[display removeObjectAtIndex:lastIndexNum.integerValue];
[lineIndexes removeLastObject];
[lineWidths removeLastObject];
//
lineUsed = 0.0;
for (NSInteger k = 0; k < lineWidths.count; k++) {
CGFloat iw = lineWidths[k].doubleValue;
lineUsed = (k == 0 ? iw : (lineUsed + itemSpacing + iw));
}
//
moreNeed = (lineUsed == 0 ? moreSize.width : (lineUsed + itemSpacing + moreSize.width));
if (moreNeed <= available) {
[display addObject:kMoreToken];
return display;
}
}
//
[display addObject:kMoreToken];
return display;
}
//
return display;
}
///
- (void)clearHistory {
[self.historyWords removeAllObjects];
@@ -91,16 +218,24 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == KBSearchSectionHistory) {
return self.historyWords.count; // 0
NSArray *list = [self currentDisplayHistory];
return list.count; //
}
return self.recommendItems.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == KBSearchSectionHistory) {
KBTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTagCellId forIndexPath:indexPath];
[cell config:self.historyWords[indexPath.item]];
return cell;
NSArray *list = [self currentDisplayHistory];
NSString *text = list[indexPath.item];
if ([text isEqualToString:kMoreToken]) {
KBHistoryMoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMoreCellId forIndexPath:indexPath];
return cell;
} else {
KBTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTagCellId forIndexPath:indexPath];
[cell config:text];
return cell;
}
}
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSkinCellId forIndexPath:indexPath];
NSDictionary *it = self.recommendItems[indexPath.item];
@@ -129,7 +264,11 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = collectionView.bounds.size.width;
if (indexPath.section == KBSearchSectionHistory) {
NSString *t = self.historyWords[indexPath.item];
NSArray *list = [self currentDisplayHistory];
NSString *t = list[indexPath.item];
if ([t isEqualToString:kMoreToken]) {
return [KBHistoryMoreCell fixedSize];
}
return [KBTagCell sizeForText:t];
}
//
@@ -168,9 +307,17 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == KBSearchSectionHistory) {
NSString *kw = self.historyWords[indexPath.item];
[self.searchBarView updateKeyword:kw];
[self performSearch:kw];
NSArray *list = [self currentDisplayHistory];
NSString *kw = list[indexPath.item];
if ([kw isEqualToString:kMoreToken]) {
//
self.historyExpanded = YES;
[self.collectionView reloadData];
} else {
[self.searchBarView updateKeyword:kw];
[self performSearch:kw];
[self openResultForKeyword:kw];
}
}
}
@@ -182,7 +329,9 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
_searchBarView.placeholder = @"Themes";
__weak typeof(self) weakSelf = self;
_searchBarView.onSearch = ^(NSString * _Nonnull keyword) {
// +
[weakSelf performSearch:keyword];
[weakSelf openResultForKeyword:keyword];
};
}
return _searchBarView;
@@ -191,7 +340,7 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
- (UIView *)titleContainer {
if (!_titleContainer) {
// 315 36 Y titleView
CGFloat width = KBFit(315.0);
CGFloat width = 315.0;
_titleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 36.0)];
_titleContainer.backgroundColor = [UIColor clearColor];
// 使 AutoLayout 315x36
@@ -212,9 +361,9 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
return _titleContainer;
}
- (UICollectionViewFlowLayout *)flowLayout {
- (UICollectionViewLeftAlignedLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_flowLayout.sectionHeadersPinToVisibleBounds = NO;
}
@@ -230,6 +379,7 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
// cell & header
[_collectionView registerClass:KBTagCell.class forCellWithReuseIdentifier:kTagCellId];
[_collectionView registerClass:KBSkinCardCell.class forCellWithReuseIdentifier:kSkinCellId];
[_collectionView registerClass:KBHistoryMoreCell.class forCellWithReuseIdentifier:kMoreCellId];
[_collectionView registerClass:KBSearchSectionHeader.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderId];
}
return _collectionView;