HTagView 4.1

HTagView 4.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2019
SPMSupports SPM

Maintained by Hao.



  • By
  • Hao

HTagView

Version License Platform

HTagView is a customized tag view sublassing UIView where tag could be either with cancel button or multiseletable.

Features

  • .cancel and .select tag types available (see below)
  • Single / Multi selection for .select tags
  • HTagViewDataSource and HTagViewDelegte protocols
  • Customized configuration
  • Supporting @IBDesignable and autolayout
  • Specific/Auto tag width
  • Specific/Auto Maximum tag width

Demo

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

HTagView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "HTagView"

Requirements

Swift Version HTagView Version
Swift 2.3 2.0.x
Swift 3.0 2.1.x
Swift 3.0 3.x
Swift 5.0 4.x

Usage

Configure HTagView

These properties support @IBDesignable as well.

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
}

Data Source

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
    }
}

Delegate

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()
    }
}

Manually select/deselect tags

tagView.selectTagAtIndex(6)
tagView.deselectTagAtIndex(3)

Author

Hao

License

HTagView is available under the MIT license. See the LICENSE file for more info.