Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feautre: 1 Tag per line #145

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Source/WSTagsField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ open class WSTagsField: UIScrollView {

return false
}

/// Each tag will occupy only 1 line. Input text field will be also moved to new line if set to true.
open var isTagPerLine: Bool = false {
didSet {
repositionViews()
}
}

open fileprivate(set) var tags = [WSTag]()
open var tagViews = [WSTagView]()
Expand Down Expand Up @@ -327,7 +334,6 @@ open class WSTagsField: UIScrollView {

open override func layoutSubviews() {
super.layoutSubviews()
repositionViews()
}

/// Take the text inside of the field and make it a Tag.
Expand Down Expand Up @@ -674,10 +680,11 @@ extension WSTagsField {

// Tag views Rects
var tagRect = CGRect.null
for tagView in tagViews {
for (index, tagView) in tagViews.enumerated() {
tagRect = CGRect(origin: CGPoint.zero, size: tagView.sizeToFit(.init(width: maxWidth, height: 0)))

if curX + tagRect.width > maxWidth {
let isNewLine = isTagPerLine ? (index > 0) : (curX + tagRect.width > maxWidth)
if isNewLine {
// Need a new line
curX = 0
curY += Constants.STANDARD_ROW_HEIGHT + spaceBetweenLines
Expand All @@ -701,7 +708,8 @@ extension WSTagsField {
var textFieldRect = CGRect.zero
textFieldRect.size.height = Constants.STANDARD_ROW_HEIGHT

if availableWidthForTextField < Constants.MINIMUM_TEXTFIELD_WIDTH {
let isNewLine = isTagPerLine ? (!tagViews.isEmpty) : (availableWidthForTextField < Constants.MINIMUM_TEXTFIELD_WIDTH)
if isNewLine {
// If in the future we add more UI elements below the tags,
// isOnFirstLine will be useful, and this calculation is important.
// So leaving it set here, and marking the warning to ignore it
Expand Down Expand Up @@ -761,7 +769,7 @@ extension WSTagsField {

if self.isScrollEnabled {
// FIXME: this isn't working. Need to think in a workaround.
//self.scrollRectToVisible(textField.frame, animated: false)
self.scrollRectToVisible(textField.frame, animated: false)
}
}

Expand Down