TagList 0.3.0

TagList 0.3.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017年1月
SwiftSwift版本3.0
SPM支持SPM

Wonder Bear维护。



TagList 0.3.0

  • xiongxiong

TagList

TagList,灵活的标签列表视图,易于使用和扩展。我爱你。

TagList

内容

特点

  • [x] 标签交互
  • [x] 自适应大小
  • [x] 易于使用
  • [x] 可定制
  • [x] 易于扩展

要求

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

安装

手动

如果您不想使用上述任何依赖项管理器,您可以将TagList手动集成到项目中。

演示

打开演示项目,构建并运行。

协议

TagListDelegate

public protocol TagListDelegate: NSObjectProtocol {

    func tagActionTriggered(tagList: TagList, action: TagAction, content: TagPresentable, index: Int)
    func tagListUpdated(tagList: TagList)
}

TagPresentable

public protocol TagPresentable {

    var tag: String { get }
    var isSelected: Bool { get set }

    func createTagContent() -> TagContent
}

TagActionable

public protocol TagActionable {

    var actionDelegate: TagActionDelegate? { get set }
}

TagActionDelegate

public protocol TagActionDelegate: NSObjectProtocol {

    func tagActionTriggered(action: TagAction)
}

TagStatable

public protocol TagStatable {

    var stateDelegate: TagStateDelegate? { get set }
}

TagStateDelegate

public protocol TagStateDelegate: NSObjectProtocol {

    func tagSelected(_ isSelected: Bool)
}

TagWrapperDelegate

protocol TagWrapperDelegate: TagActionable, TagActionDelegate, TagStatable, TagStateDelegate {

    func wrap(target: TagContent) -> TagWrapper
    func wrap(wrapper: TagWrapper) -> TagWrapper
}

用法

导入SwiftTagList

import SwiftTagList

实现 TagListDelegate [可选]

extension ViewController: TagListDelegate {

    // Called when tagList's content changed
    func tagListUpdated(tagList: TagList) {
        ...
    }

    // Tag interaction
    func tagActionTriggered(tagList: TagList, action: TagAction, content: TagPresentable, index: Int) {
        ...
    }
}

初始化 TagList

let view = TagList()
view.delegate = self
view.backgroundColor = UIColor.yellow
view.tagMargin = UIEdgeInsets(top: 3, left: 5, bottom: 3, right: 5)
view.separator.image = #imageLiteral(resourceName: "icon_arrow_right")
view.separator.size = CGSize(width: 16, height: 16)
view.separator.margin = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
...

设置框架

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        tagList.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: areaList.frame.width, height: 0))
    }

可定制

TagPresentableText, TagPresentableIcon 及 TagPresentableIconText 已实现,您可以直接使用,或者选择实现 TagPresentable 并继承 TagContent 创建自己的标签。

属性 & 方法

TagList

属性

var delegate: TagListDelegate?
var tags: [Tag] = []
var horizontalAlignment: TagHorizontalAlignment = .left | .right | .center
var verticalAlignment: TagVerticalAlignment = .left | .right | .center
var tagMargin = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
var separator: SeparatorInfo = SeparatorInfo()
var isSeparatorEnabled = false
var isAutowrap = true
var selectionMode: TagSelectionMode = .none | .single | .multiple

管理标签

func tagPresentables() -> [TagPresentable]
func selectedTagPresentables() -> [TagPresentable]

示例

let tag = Tag(content: TagPresentableText("...") {
            $0.label.font = UIFont.systemFont(ofSize: 16)
            }, onInit: {
                $0.padding = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
                $0.layer.borderColor = UIColor.cyan.cgColor
                $0.layer.borderWidth = 2
                $0.layer.cornerRadius = 5
            }, onSelect: {
            $0.backgroundColor = $0.isSelected ? UIColor.orange : UIColor.white
        })
tag.wrappers = [TagWrapperRemover(onInit: {
                    $0.deleteButton.setImage(UIImage(named: "icon_delete")).withRenderingMode(.alwaysTemplate), for: .normal)
                    $0.space = 8
                }) {
                    $0.deleteButton.tintColor = $1 ? UIColor.white : UIColor.black
              }]
tagList.tags.append(tag)

标签

初始化

init(content: TagPresentable, onInit: ((Tag) -> Void)? = nil, onSelect: ((Tag) -> Void)? = nil)

属性

var content: TagPresentable { get }
var wrappers: [TagWrapper] = []
var padding: UIEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
var isSelected: Bool

TagPresentableText

init(_ tag: String, onContentInit: ((TagContentText) -> Void)? = nil)

TagPresentableIcon

init(_ tag: String, icon: String, onContentInit: ((TagContentIcon) -> Void)? = nil)

TagPresentableIconText

init(_ tag: String, icon: String, onContentInit: ((TagContentIconText) -> Void)? = nil)

TagWrapperRemover

init(onInit: ((TagWrapperRemover) -> Void)? = nil, onSelect: ((TagWrapperRemover, Bool) -> Void)? = nil)

作者

xiongxiong, [email protected]

许可协议

TagList 根据MIT许可证提供。有关更多信息,请参阅LICENSE文件。