我用的方法是罗列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