diff --git a/keyBoard.xcodeproj/project.pbxproj b/keyBoard.xcodeproj/project.pbxproj index 11d8623..bf2367f 100644 --- a/keyBoard.xcodeproj/project.pbxproj +++ b/keyBoard.xcodeproj/project.pbxproj @@ -58,6 +58,9 @@ 048908CD2EBE373500FABA60 /* KBSearchSectionHeader.m in Sources */ = {isa = PBXBuildFile; fileRef = 048908C72EBE373500FABA60 /* KBSearchSectionHeader.m */; }; 048908CE2EBE373500FABA60 /* KBSkinCardCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 048908C92EBE373500FABA60 /* KBSkinCardCell.m */; }; 048908CF2EBE373500FABA60 /* KBTagCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 048908CB2EBE373500FABA60 /* KBTagCell.m */; }; + 048908D22EBF611D00FABA60 /* KBHistoryMoreCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 048908D12EBF611D00FABA60 /* KBHistoryMoreCell.m */; }; + 048908DA2EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 048908D82EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.m */; }; + 048908DD2EBF67EB00FABA60 /* KBSearchResultVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 048908DC2EBF67EB00FABA60 /* KBSearchResultVC.m */; }; 04A9FE0F2EB481100020DB6D /* KBHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FC97082EB31B14007BD342 /* KBHUD.m */; }; 04A9FE132EB4D0D20020DB6D /* KBFullAccessManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE112EB4D0D20020DB6D /* KBFullAccessManager.m */; }; 04A9FE162EB873C80020DB6D /* UIViewController+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE152EB873C80020DB6D /* UIViewController+Extension.m */; }; @@ -212,6 +215,12 @@ 048908C92EBE373500FABA60 /* KBSkinCardCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBSkinCardCell.m; sourceTree = ""; }; 048908CA2EBE373500FABA60 /* KBTagCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBTagCell.h; sourceTree = ""; }; 048908CB2EBE373500FABA60 /* KBTagCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBTagCell.m; sourceTree = ""; }; + 048908D02EBF611D00FABA60 /* KBHistoryMoreCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBHistoryMoreCell.h; sourceTree = ""; }; + 048908D12EBF611D00FABA60 /* KBHistoryMoreCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBHistoryMoreCell.m; sourceTree = ""; }; + 048908D72EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UICollectionViewLeftAlignedLayout.h; sourceTree = ""; }; + 048908D82EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UICollectionViewLeftAlignedLayout.m; sourceTree = ""; }; + 048908DB2EBF67EB00FABA60 /* KBSearchResultVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBSearchResultVC.h; sourceTree = ""; }; + 048908DC2EBF67EB00FABA60 /* KBSearchResultVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBSearchResultVC.m; sourceTree = ""; }; 04A9A67D2EB9E1690023B8F4 /* KBResponderUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBResponderUtils.h; sourceTree = ""; }; 04A9FE102EB4D0D20020DB6D /* KBFullAccessManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBFullAccessManager.h; sourceTree = ""; }; 04A9FE112EB4D0D20020DB6D /* KBFullAccessManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBFullAccessManager.m; sourceTree = ""; }; @@ -506,6 +515,8 @@ 048908C72EBE373500FABA60 /* KBSearchSectionHeader.m */, 048908C82EBE373500FABA60 /* KBSkinCardCell.h */, 048908C92EBE373500FABA60 /* KBSkinCardCell.m */, + 048908D02EBF611D00FABA60 /* KBHistoryMoreCell.h */, + 048908D12EBF611D00FABA60 /* KBHistoryMoreCell.m */, 048908CA2EBE373500FABA60 /* KBTagCell.h */, 048908CB2EBE373500FABA60 /* KBTagCell.m */, ); @@ -517,6 +528,8 @@ children = ( 048908C12EBE32B800FABA60 /* KBSearchVC.h */, 048908C22EBE32B800FABA60 /* KBSearchVC.m */, + 048908DB2EBF67EB00FABA60 /* KBSearchResultVC.h */, + 048908DC2EBF67EB00FABA60 /* KBSearchResultVC.m */, ); path = VC; sourceTree = ""; @@ -531,6 +544,23 @@ path = Search; sourceTree = ""; }; + 048908D32EBF618E00FABA60 /* Vender */ = { + isa = PBXGroup; + children = ( + 048908D92EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout */, + ); + path = Vender; + sourceTree = ""; + }; + 048908D92EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout */ = { + isa = PBXGroup; + children = ( + 048908D72EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.h */, + 048908D82EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.m */, + ); + path = UICollectionViewLeftAlignedLayout; + sourceTree = ""; + }; 04A9FE122EB4D0D20020DB6D /* Manager */ = { isa = PBXGroup; children = ( @@ -755,6 +785,7 @@ 04FC95BF2EB1E3B1007BD342 /* Class */ = { isa = PBXGroup; children = ( + 048908D32EBF618E00FABA60 /* Vender */, 048908C02EBE329D00FABA60 /* Search */, 048908B92EBDC11200FABA60 /* Common */, 04FC95B62EB1E3B1007BD342 /* Home */, @@ -1236,9 +1267,11 @@ 048908BC2EBE1FCB00FABA60 /* BaseViewController.m in Sources */, 04FC95D72EB1EA16007BD342 /* BaseTableView.m in Sources */, 0477BDF32EBB7B850055D639 /* KBDirectionIndicatorView.m in Sources */, + 048908D22EBF611D00FABA60 /* KBHistoryMoreCell.m in Sources */, 04FC95D82EB1EA16007BD342 /* BaseCell.m in Sources */, 0477BDF72EBC63A80055D639 /* KBTestVC.m in Sources */, 04FC95C92EB1E4C9007BD342 /* BaseNavigationController.m in Sources */, + 048908DD2EBF67EB00FABA60 /* KBSearchResultVC.m in Sources */, 047C65102EBCA8DD0035E841 /* HomeRankContentVC.m in Sources */, 047C655C2EBCD0F80035E841 /* UIView+KBShadow.m in Sources */, 048908C32EBE32B800FABA60 /* KBSearchVC.m in Sources */, @@ -1255,6 +1288,7 @@ 04FC970F2EB334F8007BD342 /* KBWebImageManager.m in Sources */, 04FC95CF2EB1E7A1007BD342 /* HomeVC.m in Sources */, A1B2D7022EB8C00100000001 /* KBLangTestVC.m in Sources */, + 048908DA2EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout.m in Sources */, 04C6EABF2EAF86530089C901 /* main.m in Sources */, 04FC95CC2EB1E780007BD342 /* BaseTabBarController.m in Sources */, 047C65502EBCBA9E0035E841 /* KBShopVC.m in Sources */, diff --git a/keyBoard/Class/Search/V/KBHistoryMoreCell.h b/keyBoard/Class/Search/V/KBHistoryMoreCell.h new file mode 100644 index 0000000..95ce74c --- /dev/null +++ b/keyBoard/Class/Search/V/KBHistoryMoreCell.h @@ -0,0 +1,18 @@ +// +// KBHistoryMoreCell.h +// keyBoard +// +// 历史记录第二行尾部的“展开更多”按钮 cell(向下箭头)。 +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface KBHistoryMoreCell : UICollectionViewCell +/// 固定尺寸建议(高度与标签一致 32) ++ (CGSize)fixedSize; +@end + +NS_ASSUME_NONNULL_END + diff --git a/keyBoard/Class/Search/V/KBHistoryMoreCell.m b/keyBoard/Class/Search/V/KBHistoryMoreCell.m new file mode 100644 index 0000000..938f653 --- /dev/null +++ b/keyBoard/Class/Search/V/KBHistoryMoreCell.m @@ -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 + diff --git a/keyBoard/Class/Search/VC/KBSearchResultVC.h b/keyBoard/Class/Search/VC/KBSearchResultVC.h new file mode 100644 index 0000000..e2da78c --- /dev/null +++ b/keyBoard/Class/Search/VC/KBSearchResultVC.h @@ -0,0 +1,21 @@ +// +// KBSearchResultVC.h +// keyBoard +// +// 搜索结果页:顶部搜索框 + 下方网格列表 +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +/// 简单的搜索结果页,供外部 push 使用 +@interface KBSearchResultVC : UIViewController + +/// 可选:进入页面时默认关键字 +@property (nonatomic, copy, nullable) NSString *defaultKeyword; + +@end + +NS_ASSUME_NONNULL_END + diff --git a/keyBoard/Class/Search/VC/KBSearchResultVC.m b/keyBoard/Class/Search/VC/KBSearchResultVC.m new file mode 100644 index 0000000..77208e6 --- /dev/null +++ b/keyBoard/Class/Search/VC/KBSearchResultVC.m @@ -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 () + +// 顶部搜索栏(复用已封装的 KBSearchBarView) +@property (nonatomic, strong) KBSearchBarView *searchBarView; + +// 结果列表 +@property (nonatomic, strong) UICollectionView *collectionView; +@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout; + +// 数据源(示例数据,实际项目中由网络返回) +@property (nonatomic, strong) NSMutableArray *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 *)resultItems { + if (!_resultItems) { + _resultItems = [NSMutableArray array]; + } + return _resultItems; +} + +@end + diff --git a/keyBoard/Class/Search/VC/KBSearchVC.m b/keyBoard/Class/Search/VC/KBSearchVC.m index 1369294..1586cd5 100644 --- a/keyBoard/Class/Search/VC/KBSearchVC.m +++ b/keyBoard/Class/Search/VC/KBSearchVC.m @@ -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 *historyWords; // 历史搜索 @property (nonatomic, strong) NSArray *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 *)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 *display = [NSMutableArray array]; + NSInteger line = 1; + CGFloat lineUsed = 0.0; // 当前行已占宽度 + // 记录当前第2行每个 item 的宽度与在 display 中的下标,便于回退 + NSMutableArray *lineWidths = [NSMutableArray array]; + NSMutableArray *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; diff --git a/keyBoard/Class/Vender/UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.h b/keyBoard/Class/Vender/UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.h new file mode 100644 index 0000000..b51c8d3 --- /dev/null +++ b/keyBoard/Class/Vender/UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.h @@ -0,0 +1,39 @@ +// Copyright (c) 2014-2023 Giovanni Lodi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/** + * Simple UICollectionViewFlowLayout that aligns the cells to the left rather than justify them + * + * Based on http://stackoverflow.com/questions/13017257/how-do-you-determine-spacing-between-cells-in-uicollectionview-flowlayout + */ + +#import + +@interface UICollectionViewLeftAlignedLayout : UICollectionViewFlowLayout + +@end + +/** + * Just a convenience protocol to keep things consistent. + * Someone could find it confusing for a delegate object to conform to UICollectionViewDelegateFlowLayout + * while using UICollectionViewLeftAlignedLayout. + */ +@protocol UICollectionViewDelegateLeftAlignedLayout + +@end diff --git a/keyBoard/Class/Vender/UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.m b/keyBoard/Class/Vender/UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.m new file mode 100644 index 0000000..a234789 --- /dev/null +++ b/keyBoard/Class/Vender/UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.m @@ -0,0 +1,116 @@ +// Copyright (c) 2014-2023 Giovanni Lodi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import "UICollectionViewLeftAlignedLayout.h" + +@interface UICollectionViewLayoutAttributes (LeftAligned) + +- (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset; + +@end + +@implementation UICollectionViewLayoutAttributes (LeftAligned) + +- (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset +{ + CGRect frame = self.frame; + frame.origin.x = sectionInset.left; + self.frame = frame; +} + +@end + +#pragma mark - + +@implementation UICollectionViewLeftAlignedLayout + +#pragma mark - UICollectionViewLayout + +- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { + NSArray *originalAttributes = [super layoutAttributesForElementsInRect:rect]; + NSMutableArray *updatedAttributes = [NSMutableArray arrayWithArray:originalAttributes]; + for (UICollectionViewLayoutAttributes *attributes in originalAttributes) { + if (!attributes.representedElementKind) { + NSUInteger index = [updatedAttributes indexOfObject:attributes]; + updatedAttributes[index] = [self layoutAttributesForItemAtIndexPath:attributes.indexPath]; + } + } + + return updatedAttributes; +} + +- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { + UICollectionViewLayoutAttributes* currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy]; + UIEdgeInsets sectionInset = [self evaluatedSectionInsetForItemAtIndex:indexPath.section]; + + BOOL isFirstItemInSection = indexPath.item == 0; + CGFloat layoutWidth = CGRectGetWidth(self.collectionView.frame) - sectionInset.left - sectionInset.right; + + if (isFirstItemInSection) { + [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset]; + return currentItemAttributes; + } + + NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; + CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame; + CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width; + CGRect currentFrame = currentItemAttributes.frame; + CGRect strecthedCurrentFrame = CGRectMake(sectionInset.left, + currentFrame.origin.y, + layoutWidth, + currentFrame.size.height); + // if the current frame, once left aligned to the left and stretched to the full collection view + // width intersects the previous frame then they are on the same line + BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame); + + if (isFirstItemInRow) { + // make sure the first item on a line is left aligned + [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset]; + return currentItemAttributes; + } + + CGRect frame = currentItemAttributes.frame; + frame.origin.x = previousFrameRightPoint + [self evaluatedMinimumInteritemSpacingForSectionAtIndex:indexPath.section]; + currentItemAttributes.frame = frame; + return currentItemAttributes; +} + +- (CGFloat)evaluatedMinimumInteritemSpacingForSectionAtIndex:(NSInteger)sectionIndex +{ + if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) { + id delegate = (id)self.collectionView.delegate; + + return [delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:sectionIndex]; + } else { + return self.minimumInteritemSpacing; + } +} + +- (UIEdgeInsets)evaluatedSectionInsetForItemAtIndex:(NSInteger)index +{ + if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) { + id delegate = (id)self.collectionView.delegate; + + return [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:index]; + } else { + return self.sectionInset; + } +} + +@end