处理详情tag的背景色
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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; }
|
||||
// 计算在给定宽度下的自动换行高度(与内部布局保持一致:行间距 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;
|
||||
|
||||
Reference in New Issue
Block a user