首先,先上效果圖。
示例一,仿天貓熱門搜索標(biāo)簽的效果:

熱門搜索.gif
示例二,動態(tài)添加和刪除標(biāo)簽

動態(tài)添加和刪除.gif
源碼連接:Github
1. 特性
- 支持AutoLayout
- 支持自定義Tag的外觀,如圓角,背景顏色,標(biāo)簽文字顏色...
- 支持動態(tài)添加和刪除Tag
- 支持單選和多選模式
2. 示例代碼
首先,添加 FMTagsView.h 和 FMTagsView.m 這兩個文件到你項目中,或者使用pod來安裝。
pod 'FMTagsView'
控件初始化示例:
FMTagsView *tagsView = [[FMTagsView alloc] initWithFrame:CGRectMake(10, 120, 300, 150)];
tagsView.contentInsets = UIEdgeInsetsZero;
tagsView.tagInsets = UIEdgeInsetsMake(5, 15, 5, 15);
tagsView.tagBorderWidth = 1;
tagsView.tagcornerRadius = 2;
tagsView.tagBorderColor = [UIColor lightGrayColor];
tagsView.tagSelectedBorderColor = [UIColor lightGrayColor];
tagsView.tagBackgroundColor = [UIColor whiteColor];
tagsView.lineSpacing = 10;
tagsView.interitemSpacing = 10;
tagsView.tagFont = [UIFont systemFontOfSize:14];
tagsView.tagTextColor = [UIColor grayColor];
tagsView.delegate = self;
[self.view addSubview:tagsView];
NSArray *dataArray = @[@"麻棉連衣裙", @"面條", @"親子裝",
@"衛(wèi)生巾", @"米", @"眉筆", @"蛋糕",
@"面包", @"洗潔精", @"咖啡速溶",
@"云南白藥牙膏", @"方便面", @"空調(diào)"];
//設(shè)置數(shù)據(jù)源
tagsView.tagsArray = dataArray;
實現(xiàn)代理方法:
//點擊標(biāo)簽處理邏輯
- (void)tagsView:(FMTagsView *)tagsView didSelectTagAtIndex:(NSUInteger)index {
NSString *selectedKey = self.dataArray[index];
UIViewController *controller = [[UIViewController alloc] init];
controller.view.backgroundColor = [UIColor whiteColor];
controller.title = selectedKey;
[self.navigationController pushViewController:controller animated:YES];
}