处理详情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

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

View File

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

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;

View File

@@ -33,7 +33,7 @@ typedef NS_ENUM(NSInteger, KBSkinDetailSection) {
@interface KBSkinDetailVC () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView; //
@property (nonatomic, strong) KBSkinBottomActionView *bottomBar; //
@property (nonatomic, copy) NSArray<NSString *> *tags; //
@property (nonatomic, copy) NSArray<KBShopThemeTagModel *> *tags; //
@property (nonatomic, copy) NSArray<KBShopThemeModel *> *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<NSString *> *tagNames = [NSMutableArray array];
NSMutableArray<KBShopThemeTagModel *> *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];
});