HTagView
HTagView 是一个自定义的标签视图子类,其中标签可以是带有取消按钮的或可多选的。
功能
- 支持
.cancel
和.select
标签类型(见下文) - 对
.select
标签进行单选/多选操作 HTagViewDataSource
和HTagViewDelegate
协议- 自定义配置
- 支持
@IBDesignable
和自动布局 - 指定/自动标签宽度
- 指定/自动最大标签宽度
演示
示例
要运行示例项目,请克隆仓库,并在示例目录中先运行 pod install
。
安装
HTagView可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod "HTagView"
要求
Swift版本 | HTagView版本 |
---|---|
Swift 2.3 | 2.0.x |
Swift 3.0 | 2.1.x |
Swift 3.0 | 3.x |
Swift 5.0 | 4.x |
使用方法
配置HTagView
这些属性支持@IBDesignable
。
override func viewDidLoad(){
super.viewDidLoad()
//.
//.
//.
// configure tagView
tagView.delegate = self
tagView.dataSource = self
tagView.marg = 20
tagView.btwTags = 20
tagView.btwLines = 20
tagView.tagFont = UIFont.systemFont(ofSize: 15)
tagView.tagMainBackColor = UIColor(red: 1, green: 130/255, blue: 103/255, alpha: 1)
tagView.tagSecondBackColor = UIColor.lightGray
tagView.tagSecondTextColor = UIColor.darkText
tagView.tagContentEdgeInsets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
tagView.tagMaximumWidth = .HTagAutoMaximumWidth
tagView.tagBorderColor = UIColor.black.cgColor
tagView.tagBorderWidth = 2
tagView.tagElevation = 40
}
数据源
class ViewController: UIViewController, HTagViewDataSource{
// .
// .
// .
// MARK: - Data
let data = ["Hey!","This","is","a","HTagView."]
// MARK: - HTagViewDataSource
func numberOfTags(_ tagView: HTagView) -> Int {
return data.count
}
func tagView(_ tagView: HTagView, titleOfTagAtIndex index: Int) -> String {
return data[index]
}
func tagView(_ tagView: HTagView, tagTypeAtIndex index: Int) -> HTagType {
return .select
// return .cancel
}
func tagView(_ tagView: HTagView, tagWidthAtIndex index: Int) -> CGFloat {
return .HTagAutoWidth
// return 150
}
}
代理
class ViewController: UIViewController, HTagViewDelegate, HTagViewDataSource {
// .
// .
// .
// MARK: - HTagViewDelegate
func tagView(_ tagView: HTagView, tagSelectionDidChange selectedIndices: [Int]) {
print("tag with indices \(selectedIndices) are selected")
}
func tagView(_ tagView: HTagView, didCancelTagAtIndex index: Int) {
print("tag with index: '\(index)' has to be removed from tagView")
data.remove(at: index)
tagView.reloadData()
}
}
手动选择/取消选择标签
tagView.selectTagAtIndex(6)
tagView.deselectTagAtIndex(3)
作者
Hao
许可证
HTagView 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。