Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions Example/WSTagsFieldExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ extension ViewController {
tagsField.onShouldAcceptTag = { field in
return field.text != "OMG"
}

tagsField.onPaste = { [weak self] text, newText in
print("Paste \(text)")
let tags = newText.components(separatedBy: " ")
self?.tagsField.addTags(tags)
return true
}
}

}
Expand Down
25 changes: 24 additions & 1 deletion Source/WSTagsField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ open class WSTagsField: UIScrollView {
*/
open var onDidChangeHeightTo: ((WSTagsField, _ height: CGFloat) -> Void)?

/**
* Called when the user pastes something.
* @return True if the pasted content has been processed. Otherwise, return false.
*/
open var onPaste: ((_ text: String, _ newText: String) -> Bool)?

// MARK: - Properties

fileprivate var oldIntrinsicContentHeight: CGFloat = 0
Expand Down Expand Up @@ -844,7 +850,10 @@ extension WSTagsField: UITextFieldDelegate {
tokenizeTextFieldText()
return false
}
return !autoCompleteText(for: textField, using: string)
var processed = autoCompleteText(for: textField, using: string)
processed = processed ? processed : handlePasting(for: textField, in: range, with: string)
//processed = processed ? processed : doSomethingElse()
return !processed
}

private func autoCompleteText(for textField: UITextField, using string: String) -> Bool {
Expand All @@ -868,6 +877,20 @@ extension WSTagsField: UITextFieldDelegate {
return false
}

private func handlePasting(for textField: UITextField, in range: NSRange, with string: String) -> Bool {
guard onPaste != nil,
string.count > 1,
let pasteboardContent = UIPasteboard.general.string,
string.contains(pasteboardContent) else { return false }

var existingText = textField.text ?? ""
let start = existingText.index(existingText.startIndex, offsetBy: range.lowerBound)
let end = existingText.index(existingText.startIndex, offsetBy: range.upperBound)
existingText.replaceSubrange(start..<end, with: string)

return onPaste?(string, existingText) == true
}

}

extension WSTagsField {
Expand Down