Tags
介绍
动态添加、修改和删除标签,您可以轻松更改标签的边距、颜色和字体。标签通过自动布局链接,不会因为尺寸变化而损坏。每当标签高度发生变化时,都可以将其高度传递给代理
要求
Tags
是用Swift 5.0编写的。兼容iOS 8.0及以上
安装
版本说明
版本 0.3.0 引入了Swift 5.0支持
版本 0.2.4 引入了Swift 4.2支持
版本 0.1.8 引入了Swift 4.0支持
直接使用Cocoapods。要安装,只需将以下行添加到您的Podfile中。
标签可以在CocoaPods上找到。要安装它,只需在Podfile中添加以下行
pod 'Tags'
# For Swift 4.2 (no longer maintained), use:
# pod 'Tags', '~> 0.2.4'
# For Swift 4.0 (no longer maintained), use:
# pod 'Tags', '~> 0.1.8'
Swift包管理器
要将Tags
通过SPM集成到Xcode 11项目中,请在项目 > Swift包中指定它
https://github.com/pikachu987/Tags
使用方法
工作流程如图所示
配置
视图
完成!
代码编辑器
import Tags
let tagView = TagsView()
self.view.addSubview(tagView)
自动布局示例
tagView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(NSLayoutConstraint(
item: self.view,
attribute: .leading,
relatedBy: .equal,
toItem: tagView,
attribute: .leading,
multiplier: 1,
constant: 0)
)
self.view.addConstraint(NSLayoutConstraint(
item: self.view,
attribute: .trailing,
relatedBy: .equal,
toItem: tagView,
attribute: .trailing,
multiplier: 1,
constant: 0)
)
self.view.addConstraint(NSLayoutConstraint(
item: self.view,
attribute: .top,
relatedBy: .equal,
toItem: tagView,
attribute: .top,
multiplier: 1,
constant: 0)
)
完成!
属性
标签
//Returned as an array of strings
tagsView.tagTextArray//get-only
//Returned as an array of TagButton
tagsView.tagArray//get-only
高度
tagsView.height//get-only
填充 & 边距
tagsView.paddingHorizontal = 6
tagsView.paddingVertical = 4
tagsView.marginHorizontal = 6
tagsView.marginVertical = 4
标签
// layer radius
tagsView.tagLayerRadius = 6
// layer width
tagsView.tagLayerWidth = 1
// layer color
tagsView.tagLayerColor = .black
// text color
tagsView.tagTitleColor = .black
// background color
tagsView.tagBackgroundColor = .white
// font
tagsView.tagFont = .systemFont(ofSize: 15)
// text longer ...
tagsView.lineBreakMode = .byTruncatingMiddle
// tag add
tagsView.tags = "Hello,Swift"
最后一个标签
// lastTag title
tagsView.lastTag = "+"
// lastTag titleColor
tagsView.lastTagTitleColor = .black
// lastTag layer Color
tagsView.lastTagLayerColor = .black
// lastTag background Color
tagsView.lastTagBackgroundColor = .white
方法
追加
tagsView.append("Hello")
tagsView.append(contentsOf: ["Hello", "World"])
tagsView.append(TagButton())
tagsView.append(contentsOf: [TagButton(), TagButton()])
设置
tagsView.set(contentsOf: ["Hello", "World"])
tagsView.set(contentsOf: [TagButton(), TagButton()])
更新
tagsView.update("Hi", at: 0)
tagsView.update(TagButton(), at: 0)
插入
tagsView.insert("World", at: 0)
tagsView.insert(TagButton(), at: 0)
删除
tagsView.remove(0)
tagsView.remove(TagButton())
tagsView.removeTags()
tagsView.removeLastTag()
tagsView.removeAll()
最后一个标签
// lastTag Button
tagsView.lastTagButton(TagButton())
重绘
// ReDraw
tagsView.redraw()
委派
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let tagsView = TagsView()
tagsView.delegate = self
}
}
extension ViewController: TagsDelegate{
// Tag Touch Action
func tagsTouchAction(_ tagsView: TagsView, tagButton: TagButton) {
}
// Last Tag Touch Action
func tagsLastTagAction(_ tagsView: TagsView, tagButton: TagButton) {
}
// TagsView Change Height
func tagsChangeHeight(_ tagsView: TagsView, height: CGFloat) {
}
}
自定义
标签按钮自定义
let button = TagButton()
button.setTitle("Tag", for: .normal)
let options = ButtonOptions(
layerColor: UIColor.black, // layer Color
layerRadius: 6.0, // layer Radius
layerWidth: 1.0, // layer Width
tagTitleColor: UIColor.black, // title Color
tagFont: UIFont.systemFont(ofSize: 15), // Font
tagBackgroundColor: UIColor.white, // Background Color
lineBreakMode: NSLineBreakMode.byTruncatingMiddle //break Mode
)
button.setEntity(options)
tagsView.append(button)
最后一个标签按钮自定义
let button = TagButton()
button.setTitle("Tag", for: .normal)
let options = ButtonOptions(
layerColor: UIColor.black, // layer Color
layerRadius: 6.0, // layer Radius
layerWidth: 1.0, // layer Width
tagTitleColor: UIColor.black, // title Color
tagFont: UIFont.systemFont(ofSize: 15), // Font
tagBackgroundColor: UIColor.white, // Background Color
lineBreakMode: NSLineBreakMode.byTruncatingMiddle //break Mode
)
button.setEntity(options)
tagsView.lastTagButton(TagButton())
自定义标签布局
self.tagsView.tagLayerColor = .clear
self.tagsView.marginHorizontal = 0
self.tagsView.paddingHorizontal = 0
self.tagsView.marginVertical = 0
self.tagsView.paddingVertical = 1
// id or nickname
let idButton = TagButton()
idButton.setTitle("pikachu987", for: .normal)
let options = ButtonOptions(
layerColor: UIColor.clear,
tagTitleColor: UIColor.black,
tagFont: UIFont.boldSystemFont(ofSize: 15),
tagBackgroundColor: UIColor.clear)
idButton.setEntity(options)
self.tagsView.append(idButton)
// array data
let array = ["Hello Instagram Tag Example", "@Lorem", "ipsum", "@dolor", "sit", "@er", "elit", "@lamet, consectetaur", "@cillium", "@adipisicing", "@pecu, sed", "@do", "@eiusmod", "tempor", "@incididunt", "ut", "@labore", "@et", "@dolore", "@magna", "@aliqua.", "Ut", "@enim", "@ad", "@minim", "@veniam", "@quis", "@nostrud", "@exercitation", "@ullamco", "@laboris", "@nisi", "@ut", "@aliquip", "@ex", "@ea", "@commodo", "@consequat.", "@Duis", "@aute", "@irure", "@dolor", "@in", "@reprehenderit", "@in", "@voluptate", "@velit", "@esse", "@cillum", "@dolore", "@eu", "@fugiat", "@nulla", "@pariatur.", "@Excepteur", "@sint", "@occaecat", "@cupidatat", "@non", "@proident,", "@sunt", "@in", "@culpa", "@qui", "@officia", "@deserunt", "@mollit", "@anim", "@id", "@est", "@laborum.", "@Nam", "@liber", "@te", "@conscient", "@to", "@factor", "@tum", "@poen", "@legum", "@odioque", "@civiuda."]
let tags = array.enumerated().map({ (tag) -> TagButton in
let titleColor = tag.element.hasPrefix("@") ? UIColor(red: 33/255, green: 100/255, blue: 255/255, alpha: 1) : .black
let tagButton = TagButton()
tagButton.setTitle(tag.element, for: .normal)
let options = ButtonOptions(
layerColor: UIColor.clear,
tagTitleColor: titleColor,
tagFont: UIFont.systemFont(ofSize: 15),
tagBackgroundColor: UIColor.clear)
tagButton.setEntity(options)
return tagButton
})
self.tagsView.append(contentsOf: tags)
作者
pikachu987, [email protected]
许可证
标签基于MIT许可证可用。更多信息请查看LICENSE文件。