From 0a725e845e7b379a6019b04198ad44e426b707bc Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Tue, 23 Dec 2025 20:56:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E8=AF=A6=E6=83=85tag?= =?UTF-8?q?=E7=9A=84=E8=83=8C=E6=99=AF=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keyBoard/Class/Categories/UIColor+Extension.h | 1 + keyBoard/Class/Categories/UIColor+Extension.m | 21 +++++++++++++++++++ keyBoard/Class/Me/V/KBSkinDetailTagCell.h | 8 ++++--- keyBoard/Class/Me/V/KBSkinDetailTagCell.m | 11 +++++++--- keyBoard/Class/Me/V/KBSkinTagsContainerCell.h | 8 ++++--- keyBoard/Class/Me/V/KBSkinTagsContainerCell.m | 13 ++++++------ keyBoard/Class/Shop/VC/KBSkinDetailVC.m | 10 ++++----- 7 files changed, 52 insertions(+), 20 deletions(-) diff --git a/keyBoard/Class/Categories/UIColor+Extension.h b/keyBoard/Class/Categories/UIColor+Extension.h index 2584919..5beb164 100644 --- a/keyBoard/Class/Categories/UIColor+Extension.h +++ b/keyBoard/Class/Categories/UIColor+Extension.h @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @interface UIColor (Extension) + (UIColor *)colorWithHex:(int)hexValue; ++ (nullable UIColor *)colorWithHexString:(NSString *)hexString; @end NS_ASSUME_NONNULL_END diff --git a/keyBoard/Class/Categories/UIColor+Extension.m b/keyBoard/Class/Categories/UIColor+Extension.m index c7349da..2a719ae 100644 --- a/keyBoard/Class/Categories/UIColor+Extension.m +++ b/keyBoard/Class/Categories/UIColor+Extension.m @@ -20,4 +20,25 @@ { return [UIColor colorWithHex:hexValue alpha:1.0]; } + ++ (UIColor *)colorWithHexString:(NSString *)hexString +{ + if (hexString.length == 0) { return nil; } + NSString *clean = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; + if ([clean hasPrefix:@"#"]) { + clean = [clean substringFromIndex:1]; + } else if ([clean hasPrefix:@"0X"]) { + clean = [clean substringFromIndex:2]; + } + if (clean.length != 6 && clean.length != 8) { return nil; } + unsigned long long value = 0; + NSScanner *scanner = [NSScanner scannerWithString:clean]; + if (![scanner scanHexLongLong:&value]) { return nil; } + if (clean.length == 6) { + return [UIColor colorWithHex:(int)value]; + } + CGFloat alpha = ((value & 0xFF000000) >> 24) / 255.0; + int rgb = (int)(value & 0xFFFFFF); + return [UIColor colorWithHex:rgb alpha:alpha]; +} @end diff --git a/keyBoard/Class/Me/V/KBSkinDetailTagCell.h b/keyBoard/Class/Me/V/KBSkinDetailTagCell.h index b2be094..5e9e5b6 100644 --- a/keyBoard/Class/Me/V/KBSkinDetailTagCell.h +++ b/keyBoard/Class/Me/V/KBSkinDetailTagCell.h @@ -9,10 +9,12 @@ NS_ASSUME_NONNULL_BEGIN +@class KBShopThemeTagModel; + @interface KBSkinDetailTagCell : UICollectionViewCell -- (void)config:(NSString *)text; -/// 根据文案计算自适应宽度(外部布局用) -+ (CGSize)sizeForText:(NSString *)text; +- (void)configWithTag:(KBShopThemeTagModel *)tag; +/// 根据标签计算自适应宽度(外部布局用) ++ (CGSize)sizeForTag:(KBShopThemeTagModel *)tag; @end NS_ASSUME_NONNULL_END diff --git a/keyBoard/Class/Me/V/KBSkinDetailTagCell.m b/keyBoard/Class/Me/V/KBSkinDetailTagCell.m index 2a1c16c..06d1611 100644 --- a/keyBoard/Class/Me/V/KBSkinDetailTagCell.m +++ b/keyBoard/Class/Me/V/KBSkinDetailTagCell.m @@ -6,6 +6,8 @@ // #import "KBSkinDetailTagCell.h" +#import "KBShopThemeTagModel.h" +#import "UIColor+Extension.h" @interface KBSkinDetailTagCell () @property (nonatomic, strong) UILabel *titleLabel; @end @@ -23,11 +25,14 @@ return self; } -- (void)config:(NSString *)text { - self.titleLabel.text = text ?: @""; +- (void)configWithTag:(KBShopThemeTagModel *)tag { + NSString *text = tag.label ?: @""; + self.titleLabel.text = text; + self.contentView.backgroundColor = [UIColor colorWithHexString:tag.color]; } -+ (CGSize)sizeForText:(NSString *)text { ++ (CGSize)sizeForTag:(KBShopThemeTagModel *)tag { + NSString *text = tag.label ?: @""; if (text.length == 0) { return CGSizeMake(40, 24); } CGSize s = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}]; // 两侧内边距 12 + 12,高度固定 32 diff --git a/keyBoard/Class/Me/V/KBSkinTagsContainerCell.h b/keyBoard/Class/Me/V/KBSkinTagsContainerCell.h index 3cf4f72..aedc34a 100644 --- a/keyBoard/Class/Me/V/KBSkinTagsContainerCell.h +++ b/keyBoard/Class/Me/V/KBSkinTagsContainerCell.h @@ -9,12 +9,14 @@ NS_ASSUME_NONNULL_BEGIN +@class KBShopThemeTagModel; + @interface KBSkinTagsContainerCell : UICollectionViewCell @property (nonatomic, strong) UICollectionView *tagsView; // 内部标签列表 -@property (nonatomic, copy) NSArray *tags; // 标签文案 -- (void)configWithTags:(NSArray *)tags; +@property (nonatomic, copy) NSArray *tags; // 标签数据 +- (void)configWithTags:(NSArray *)tags; /// 根据给定宽度,计算该容器需要的高度(用于外部 sizeForItem) -+ (CGFloat)heightForTags:(NSArray *)tags width:(CGFloat)width; ++ (CGFloat)heightForTags:(NSArray *)tags width:(CGFloat)width; @end NS_ASSUME_NONNULL_END diff --git a/keyBoard/Class/Me/V/KBSkinTagsContainerCell.m b/keyBoard/Class/Me/V/KBSkinTagsContainerCell.m index 7f378bc..03f538a 100644 --- a/keyBoard/Class/Me/V/KBSkinTagsContainerCell.m +++ b/keyBoard/Class/Me/V/KBSkinTagsContainerCell.m @@ -8,6 +8,7 @@ #import "KBSkinTagsContainerCell.h" #import "KBSkinDetailTagCell.h" #import "UICollectionViewLeftAlignedLayout.h" +#import "KBShopThemeTagModel.h" static NSString * const kInnerTagCellId = @"kInnerTagCellId"; @implementation KBSkinTagsContainerCell @@ -23,7 +24,7 @@ static NSString * const kInnerTagCellId = @"kInnerTagCellId"; return self; } -- (void)configWithTags:(NSArray *)tags { +- (void)configWithTags:(NSArray *)tags { self.tags = tags; [self.tagsView reloadData]; } @@ -35,25 +36,25 @@ static NSString * const kInnerTagCellId = @"kInnerTagCellId"; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBSkinDetailTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kInnerTagCellId forIndexPath:indexPath]; - [cell config:self.tags[indexPath.item]]; + [cell configWithTag:self.tags[indexPath.item]]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { // 根据文案自适应宽度,高度固定 32 - return [KBSkinDetailTagCell sizeForText:self.tags[indexPath.item]]; + return [KBSkinDetailTagCell sizeForTag:self.tags[indexPath.item]]; } #pragma mark - Height Helper -+ (CGFloat)heightForTags:(NSArray *)tags width:(CGFloat)width { ++ (CGFloat)heightForTags:(NSArray *)tags width:(CGFloat)width { if (tags.count == 0) { return 0; } // 计算在给定宽度下的自动换行高度(与内部布局保持一致:行间距 8,item 间距 8,sectionInsets = {0,16,0,16}) CGFloat leftRight = 16 * 2; // 外部 VC 会给整体 sectionInset=16,这里额外留一点安全边距 CGFloat maxWidth = width - leftRight; CGFloat x = 0; CGFloat rows = 1; - for (NSString *t in tags) { - CGSize s = [KBSkinDetailTagCell sizeForText:t]; + for (KBShopThemeTagModel *tag in tags) { + CGSize s = [KBSkinDetailTagCell sizeForTag:tag]; CGFloat iw = ceil(s.width); if (x == 0) { x = iw; diff --git a/keyBoard/Class/Shop/VC/KBSkinDetailVC.m b/keyBoard/Class/Shop/VC/KBSkinDetailVC.m index d7c0d22..4b53bad 100644 --- a/keyBoard/Class/Shop/VC/KBSkinDetailVC.m +++ b/keyBoard/Class/Shop/VC/KBSkinDetailVC.m @@ -33,7 +33,7 @@ typedef NS_ENUM(NSInteger, KBSkinDetailSection) { @interface KBSkinDetailVC () @property (nonatomic, strong) UICollectionView *collectionView; // 主列表 @property (nonatomic, strong) KBSkinBottomActionView *bottomBar; // 底部操作条 -@property (nonatomic, copy) NSArray *tags; // 标签数据 +@property (nonatomic, copy) NSArray *tags; // 标签数据 @property (nonatomic, copy) NSArray *recommendedThemes; // 底部网格数据 @property (nonatomic, strong) KBShopVM *shopVM; @property (nonatomic, strong, nullable) KBShopThemeDetailModel *detailModel; @@ -344,13 +344,13 @@ typedef NS_ENUM(NSInteger, KBSkinDetailSection) { return; } weakSelf.detailModel = detail; - NSMutableArray *tagNames = [NSMutableArray array]; + NSMutableArray *tags = [NSMutableArray array]; for (KBShopThemeTagModel *tag in detail.themeTag) { - if (tag.label.length) { - [tagNames addObject:tag.label]; + if (tag.label.length > 0) { + [tags addObject:tag]; } } - weakSelf.tags = tagNames.copy; + weakSelf.tags = tags.copy; [weakSelf updateBottomBarAppearance]; [weakSelf refreshHeaderAndTags]; });