KenTagSelector
由 Objective-C 编写的标签选择器。(一个类似网易频道选择器的标签选择界面)
功能特性
- 用户可选择单个栏目或将其加入选择列表。
- 可以设置固定栏目,固定栏目不能被删除。
- 在已选栏目列表中,长按标签支持拖拽排序。
- 已适配 iOS 13 的暗黑模式。
安装方法
使用 CocoaPods (推荐)
- 在 Podfile 中添加一行 pod 'KenTagSelector'。
- 执行 pod install 或 pod update。
手工安装
- 点击“Code”按钮,选择“Download ZIP”,将代码下载到本地。
- 解压缩 ZIP 文件,进入解压后的目录,使用 Xcode 打开 KenTagSelector.xcodeproj 工程。
- 按 Command+B 编译,编译成功后,在 Products 目录下会生成 KenTagSelector.framework。
- 在 KenTagSelector.framework 上右键点击,选择“Show in Finder”,即可找到编译后的 framework 文件。
- 将 KenTagSelector.framework 插入到您的项目中。
使用说明
接入方式非常简单,直接粘贴代码即可。在需要展示栏目选择器的ViewController中,可以按照以下格式编写代码。选择结果通过两个Block返回(分别对应单选和多选)。
#import "ViewController.h"
#import <KenTagSelector/KenTagSelector.h>
@interface ViewController ()
{
__block NSMutableArray *selectedTagArray;
__block NSMutableArray *otherTagArray;
NSArray *residentArray;
NSString *focusTitle;
}
@property (weak, nonatomic) IBOutlet UILabel *labelSelected;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//已选栏目列表
selectedTagArray = @[@"关注",@"头条",@"抗疫",@"北京",@"科技",@"汽车",@"社会",@"军事",@"知否",@"历史",@"要闻",@"财经",@"军事"].mutableCopy;
//备选栏目列表
otherTagArray = @[@"独家",@"航空",@"娱乐",@"影视",@"音乐",@"股票",@"体育",@"CBA",@"冬奥",@"手机",@"数码",@"房产",@"游戏",@"旅游",@"健康",@"亲子",@"时尚",@"艺术",@"星座",@"段子",@"跟帖",@"图片",@"萌宠",@"圈子"].mutableCopy;
//固定栏目列表(可选),注意:这个列表里的文字必须在“已选栏目列表”中存在,否则设了也没有用
residentArray = @[@"关注", @"头条"];
//焦点栏目(可选)
focusTitle = @"头条";
}
//“选择栏目列表”按钮被点击的处理
- (IBAction)onBtnSelectClicked:(id)sender {
//创建栏目选择器,输入已选栏目列表和备选栏目列表
TagSelectorVC *selectorVC = [[TagSelectorVC alloc] initWithSelectedTags:selectedTagArray andOtherTags:otherTagArray];
//设置固定栏目(可选步骤)
selectorVC.residentTagStringArray = residentArray;
//设置焦点栏目(可选步骤)
selectorVC.focusTitle = focusTitle;
//弹出栏目选择界面
[self presentViewController:selectorVC animated:YES completion:^{}];
//返回所有选中栏目
__block NSMutableString *strChannels = [NSMutableString new];
selectorVC.choosedTags = ^(NSArray *selectedTags, NSArray *otherTags) {
self->selectedTagArray = @[].mutableCopy;
self->otherTagArray = @[].mutableCopy;
for (Channel *channel in otherTags) {
[self->otherTagArray addObject:channel.title];
}
for (Channel *channel in selectedTags) {
[self->selectedTagArray addObject:channel.title];
[strChannels appendString:channel.title];
[strChannels appendString:@", "];
}
self->_labelSelected.text = strChannels; //将选中的栏目在Label中显示
};
//用户点击了某个栏目的处理Block
selectorVC.activeTag = ^(Channel *channel, NSInteger index) {
[strChannels appendString:channel.title];
self->_labelSelected.text = strChannels; //将选中的栏目在Label中显示
NSLog(@"The index of Active Tag is: %ld", index);
};
}
@end