iOS - tagView 砖块墙 标签

1 篇文章 0 订阅
1 篇文章 0 订阅

我用的方法是罗列UIButton,然后通过计算进行布局

直接上代码

- (void)addTagsWithDictionary:(NSDictionary *)dictionary{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat x = 16;
    CGFloat y = 20;
    //tags是标签列表
    //SNMyTapRandomColorTool是随机生成颜色的工具
    for (NSString *string in tags) {
        SNMyTapColor *tapColor = [[SNMyTapRandomColorTool shareInstance] randColor];

        UIButton *tagButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tagButton.layer.cornerRadius = 17;
        tagButton.clipsToBounds = YES;
        [tagButton setTitleColor:tapColor.strColor forState:UIControlStateNormal];
        tagButton.backgroundColor = tapColor.bgColor;
        [tagButton setTitle:string forState:UIControlStateNormal];
        tagButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
        [tagButton addTarget:self action:@selector(pressTag:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:tagButton];
        
        CGFloat stringWidth = [self caculateTextWidthWithString:string];
        if (x + stringWidth + 5 <= GETSCREEN_WIDTH - 16) {
            tagButton.frame = CGRectMake(x, y, stringWidth, 34);
            x += (stringWidth + 5);
        }else{
            x = 16;
            y += 44;
            tagButton.frame = CGRectMake(x, y, stringWidth, 34);
            x += stringWidth + 5;
        }
    }
    self.tapViewMaxY = y + 34;
}

- (void)pressTag:(UIButton *)button{
    
}

- (CGFloat)caculateTextWidthWithString:(NSString *)string{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 30) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    return rect.size.width + 40;
}

SNMyTapRandomColorTool

这是一个颜色随机生成器,我预置了7个颜色,每次通过tool生成的颜色前后不会重复,其中用到了arc4random(),用它来生成0-6之间的随机整数

#import <Foundation/Foundation.h>
#import "SNMyTapColor.h"

NS_ASSUME_NONNULL_BEGIN

@interface SNMyTapRandomColorTool : NSObject

+ (SNMyTapRandomColorTool *)shareInstance;
- (SNMyTapColor *)randColor;

@end

NS_ASSUME_NONNULL_END


#import "SNMyTapRandomColorTool.h"
#import "UIColor+YYAdd.h"

@interface SNMyTapRandomColorTool ()

@property (nonatomic, strong) NSArray *strColorList;
@property (nonatomic, strong) NSArray *bgColorList;
@property (nonatomic, assign) int lastRandomColor;

@end

@implementation SNMyTapRandomColorTool

+ (SNMyTapRandomColorTool *)shareInstance {
    static SNMyTapRandomColorTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SNMyTapRandomColorTool alloc] init];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        self.strColorList = [NSArray arrayWithObjects:
                             [UIColor colorWithHexString:@"85dff2"],
                             [UIColor colorWithHexString:@"86e3d1"],
                             [UIColor colorWithHexString:@"ffd9d9"],
                             [UIColor colorWithHexString:@"9f86ff"],
                             [UIColor colorWithHexString:@"ffe148"],
                             [UIColor colorWithHexString:@"fa9393"],
                             [UIColor colorWithHexString:@"ff9c48"],nil];
        
        self.bgColorList = [NSArray arrayWithObjects:
                            [UIColor colorWithHexString:@"effafd"],
                            [UIColor colorWithHexString:@"effaf7"],
                            [UIColor colorWithHexString:@"fdf4f4"],
                            [UIColor colorWithHexString:@"efecfd"],
                            [UIColor colorWithHexString:@"fefae5"],
                            [UIColor colorWithHexString:@"fbefee"],
                            [UIColor colorWithHexString:@"fcefe4"],nil];
        self.lastRandomColor = 3;
    }
    return self;
}

- (SNMyTapColor *)randColor {
    int i = arc4random() % 7;
    if (i == self.lastRandomColor) {
        return [self randColor];
    }
    
    self.lastRandomColor = i;
    SNMyTapColor *tapColor = [[SNMyTapColor alloc] init];
    tapColor.strColor = self.strColorList[i];
    tapColor.bgColor = self.bgColorList[i];
    
    return tapColor;
}

@end

SNMyTapColor


@interface SNMyTapColor : NSObject

@property (nonatomic, strong) UIColor *strColor;
@property (nonatomic, strong) UIColor *bgColor;

@end

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值