处理详情tag的背景色

This commit is contained in:
2025-12-23 20:56:00 +08:00
parent 6a539dc3c5
commit 0a725e845e
7 changed files with 52 additions and 20 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -9,12 +9,14 @@
NS_ASSUME_NONNULL_BEGIN
@class KBShopThemeTagModel;
@interface KBSkinTagsContainerCell : UICollectionViewCell <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *tagsView; // 内部标签列表
@property (nonatomic, copy) NSArray<NSString *> *tags; // 标签文案
- (void)configWithTags:(NSArray<NSString *> *)tags;
@property (nonatomic, copy) NSArray<KBShopThemeTagModel *> *tags; // 标签数据
- (void)configWithTags:(NSArray<KBShopThemeTagModel *> *)tags;
/// 根据给定宽度,计算该容器需要的高度(用于外部 sizeForItem
+ (CGFloat)heightForTags:(NSArray<NSString *> *)tags width:(CGFloat)width;
+ (CGFloat)heightForTags:(NSArray<KBShopThemeTagModel *> *)tags width:(CGFloat)width;
@end
NS_ASSUME_NONNULL_END

View File

@@ -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<NSString *> *)tags {
- (void)configWithTags:(NSArray<KBShopThemeTagModel *> *)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<NSString *> *)tags width:(CGFloat)width {
+ (CGFloat)heightForTags:(NSArray<KBShopThemeTagModel *> *)tags width:(CGFloat)width {
if (tags.count == 0) { return 0; }
// 8item 8sectionInsets = {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;