Skip to content

Commit

Permalink
Add restoration text selection after changing mask
Browse files Browse the repository at this point in the history
  • Loading branch information
seaburg committed Oct 25, 2016
1 parent ef8e09a commit e2e7f4f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions FormattedTextField/FormattedTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ open class FormattedTextField: UITextField {
@IBInspectable open var textMask: String? {
didSet(oldMask) {
var cursorPosition: Int = 0
if let selectedRange = selectedCharachtersRange, let text = text {
cursorPosition = text.distance(from: text.startIndex, to: selectedRange.lowerBound)
} else {
cursorPosition = 0
}

let unformattedText = unformatterText(fromText: (text ?? ""), textMask: oldMask, cursorPosition: &cursorPosition)
let newFormattedText = formattedText(fromText: unformattedText, textMask: textMask, cursorPosition: &cursorPosition)
text = newFormattedText

if selectedTextRange != nil {
let cursorIndex = newFormattedText.index(newFormattedText.startIndex, offsetBy: cursorPosition, limitedBy: newFormattedText.endIndex) ?? newFormattedText.endIndex
selectedCharachtersRange = cursorIndex..<cursorIndex
}
}
}

Expand Down

0 comments on commit e2e7f4f

Please sign in to comment.