Skip to content

Latest commit

 

History

History
124 lines (97 loc) · 3.16 KB

README.md

File metadata and controls

124 lines (97 loc) · 3.16 KB

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

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"
Swift Version HTagView Version
Swift 2.3 2.0.x
Swift 3.0 2.1.0 or later

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.fontSize = 15
    tagView.tagMainBackColor = UIColor(red: 1, green: 130/255, blue: 103/255, alpha: 1)
    tagView.tagSecondBackColor = UIColor.lightGrayColor()
    tagView.tagSecondTextColor = UIColor.darkTextColor()
    tagView.tagContentEdgeInsets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
    tagView.tagBorderColor = UIColor.blackColor().CGColor
    tagView.tagBorderWidth = 2
}

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

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.removeAtIndex(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.