处理详情tag的背景色
This commit is contained in:
@@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
|
|
||||||
@interface UIColor (Extension)
|
@interface UIColor (Extension)
|
||||||
+ (UIColor *)colorWithHex:(int)hexValue;
|
+ (UIColor *)colorWithHex:(int)hexValue;
|
||||||
|
+ (nullable UIColor *)colorWithHexString:(NSString *)hexString;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
|||||||
@@ -20,4 +20,25 @@
|
|||||||
{
|
{
|
||||||
return [UIColor colorWithHex:hexValue alpha:1.0];
|
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
|
@end
|
||||||
|
|||||||
@@ -9,10 +9,12 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@class KBShopThemeTagModel;
|
||||||
|
|
||||||
@interface KBSkinDetailTagCell : UICollectionViewCell
|
@interface KBSkinDetailTagCell : UICollectionViewCell
|
||||||
- (void)config:(NSString *)text;
|
- (void)configWithTag:(KBShopThemeTagModel *)tag;
|
||||||
/// 根据文案计算自适应宽度(外部布局用)
|
/// 根据标签计算自适应宽度(外部布局用)
|
||||||
+ (CGSize)sizeForText:(NSString *)text;
|
+ (CGSize)sizeForTag:(KBShopThemeTagModel *)tag;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#import "KBSkinDetailTagCell.h"
|
#import "KBSkinDetailTagCell.h"
|
||||||
|
#import "KBShopThemeTagModel.h"
|
||||||
|
#import "UIColor+Extension.h"
|
||||||
@interface KBSkinDetailTagCell ()
|
@interface KBSkinDetailTagCell ()
|
||||||
@property (nonatomic, strong) UILabel *titleLabel;
|
@property (nonatomic, strong) UILabel *titleLabel;
|
||||||
@end
|
@end
|
||||||
@@ -23,11 +25,14 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)config:(NSString *)text {
|
- (void)configWithTag:(KBShopThemeTagModel *)tag {
|
||||||
self.titleLabel.text = text ?: @"";
|
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); }
|
if (text.length == 0) { return CGSizeMake(40, 24); }
|
||||||
CGSize s = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
|
CGSize s = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
|
||||||
// 两侧内边距 12 + 12,高度固定 32
|
// 两侧内边距 12 + 12,高度固定 32
|
||||||
|
|||||||
@@ -9,12 +9,14 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@class KBShopThemeTagModel;
|
||||||
|
|
||||||
@interface KBSkinTagsContainerCell : UICollectionViewCell <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
@interface KBSkinTagsContainerCell : UICollectionViewCell <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||||
@property (nonatomic, strong) UICollectionView *tagsView; // 内部标签列表
|
@property (nonatomic, strong) UICollectionView *tagsView; // 内部标签列表
|
||||||
@property (nonatomic, copy) NSArray<NSString *> *tags; // 标签文案
|
@property (nonatomic, copy) NSArray<KBShopThemeTagModel *> *tags; // 标签数据
|
||||||
- (void)configWithTags:(NSArray<NSString *> *)tags;
|
- (void)configWithTags:(NSArray<KBShopThemeTagModel *> *)tags;
|
||||||
/// 根据给定宽度,计算该容器需要的高度(用于外部 sizeForItem)
|
/// 根据给定宽度,计算该容器需要的高度(用于外部 sizeForItem)
|
||||||
+ (CGFloat)heightForTags:(NSArray<NSString *> *)tags width:(CGFloat)width;
|
+ (CGFloat)heightForTags:(NSArray<KBShopThemeTagModel *> *)tags width:(CGFloat)width;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#import "KBSkinTagsContainerCell.h"
|
#import "KBSkinTagsContainerCell.h"
|
||||||
#import "KBSkinDetailTagCell.h"
|
#import "KBSkinDetailTagCell.h"
|
||||||
#import "UICollectionViewLeftAlignedLayout.h"
|
#import "UICollectionViewLeftAlignedLayout.h"
|
||||||
|
#import "KBShopThemeTagModel.h"
|
||||||
static NSString * const kInnerTagCellId = @"kInnerTagCellId";
|
static NSString * const kInnerTagCellId = @"kInnerTagCellId";
|
||||||
|
|
||||||
@implementation KBSkinTagsContainerCell
|
@implementation KBSkinTagsContainerCell
|
||||||
@@ -23,7 +24,7 @@ static NSString * const kInnerTagCellId = @"kInnerTagCellId";
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)configWithTags:(NSArray<NSString *> *)tags {
|
- (void)configWithTags:(NSArray<KBShopThemeTagModel *> *)tags {
|
||||||
self.tags = tags;
|
self.tags = tags;
|
||||||
[self.tagsView reloadData];
|
[self.tagsView reloadData];
|
||||||
}
|
}
|
||||||
@@ -35,25 +36,25 @@ static NSString * const kInnerTagCellId = @"kInnerTagCellId";
|
|||||||
}
|
}
|
||||||
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||||
KBSkinDetailTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kInnerTagCellId forIndexPath:indexPath];
|
KBSkinDetailTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kInnerTagCellId forIndexPath:indexPath];
|
||||||
[cell config:self.tags[indexPath.item]];
|
[cell configWithTag:self.tags[indexPath.item]];
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||||
// 根据文案自适应宽度,高度固定 32
|
// 根据文案自适应宽度,高度固定 32
|
||||||
return [KBSkinDetailTagCell sizeForText:self.tags[indexPath.item]];
|
return [KBSkinDetailTagCell sizeForTag:self.tags[indexPath.item]];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Height Helper
|
#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; }
|
if (tags.count == 0) { return 0; }
|
||||||
// 计算在给定宽度下的自动换行高度(与内部布局保持一致:行间距 8,item 间距 8,sectionInsets = {0,16,0,16})
|
// 计算在给定宽度下的自动换行高度(与内部布局保持一致:行间距 8,item 间距 8,sectionInsets = {0,16,0,16})
|
||||||
CGFloat leftRight = 16 * 2; // 外部 VC 会给整体 sectionInset=16,这里额外留一点安全边距
|
CGFloat leftRight = 16 * 2; // 外部 VC 会给整体 sectionInset=16,这里额外留一点安全边距
|
||||||
CGFloat maxWidth = width - leftRight;
|
CGFloat maxWidth = width - leftRight;
|
||||||
CGFloat x = 0;
|
CGFloat x = 0;
|
||||||
CGFloat rows = 1;
|
CGFloat rows = 1;
|
||||||
for (NSString *t in tags) {
|
for (KBShopThemeTagModel *tag in tags) {
|
||||||
CGSize s = [KBSkinDetailTagCell sizeForText:t];
|
CGSize s = [KBSkinDetailTagCell sizeForTag:tag];
|
||||||
CGFloat iw = ceil(s.width);
|
CGFloat iw = ceil(s.width);
|
||||||
if (x == 0) {
|
if (x == 0) {
|
||||||
x = iw;
|
x = iw;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ typedef NS_ENUM(NSInteger, KBSkinDetailSection) {
|
|||||||
@interface KBSkinDetailVC () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
@interface KBSkinDetailVC () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||||
@property (nonatomic, strong) UICollectionView *collectionView; // 主列表
|
@property (nonatomic, strong) UICollectionView *collectionView; // 主列表
|
||||||
@property (nonatomic, strong) KBSkinBottomActionView *bottomBar; // 底部操作条
|
@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, copy) NSArray<KBShopThemeModel *> *recommendedThemes; // 底部网格数据
|
||||||
@property (nonatomic, strong) KBShopVM *shopVM;
|
@property (nonatomic, strong) KBShopVM *shopVM;
|
||||||
@property (nonatomic, strong, nullable) KBShopThemeDetailModel *detailModel;
|
@property (nonatomic, strong, nullable) KBShopThemeDetailModel *detailModel;
|
||||||
@@ -344,13 +344,13 @@ typedef NS_ENUM(NSInteger, KBSkinDetailSection) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
weakSelf.detailModel = detail;
|
weakSelf.detailModel = detail;
|
||||||
NSMutableArray<NSString *> *tagNames = [NSMutableArray array];
|
NSMutableArray<KBShopThemeTagModel *> *tags = [NSMutableArray array];
|
||||||
for (KBShopThemeTagModel *tag in detail.themeTag) {
|
for (KBShopThemeTagModel *tag in detail.themeTag) {
|
||||||
if (tag.label.length) {
|
if (tag.label.length > 0) {
|
||||||
[tagNames addObject:tag.label];
|
[tags addObject:tag];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
weakSelf.tags = tagNames.copy;
|
weakSelf.tags = tags.copy;
|
||||||
[weakSelf updateBottomBarAppearance];
|
[weakSelf updateBottomBarAppearance];
|
||||||
[weakSelf refreshHeaderAndTags];
|
[weakSelf refreshHeaderAndTags];
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user