moved extension

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-05-09 10:05:38 -05:00
parent 4ed2b3c894
commit a71e391050
2 changed files with 23 additions and 23 deletions

View File

@ -348,26 +348,3 @@ extension String {
}
}
extension UITextField {
internal func cursorPosition(range: NSRange, replacementString string: String, rawNumber: String, formattedNumber: String) -> UITextPosition? {
let start = range.location
let length = string.count
let newCursorLocation = start + length
// Adjust the cursor position to skip over formatting characters
var formattedCharacterCount = 0
for (index, character) in formattedNumber.enumerated() {
if index >= newCursorLocation + formattedCharacterCount {
break
}
if !character.isNumber {
formattedCharacterCount += 1
}
}
let finalCursorLocation = min(newCursorLocation + formattedCharacterCount, formattedNumber.count)
return position(from: beginningOfDocument, offset: finalCursorLocation)
}
}

View File

@ -82,3 +82,26 @@ open class TextField: UITextField {
return success
}
}
extension UITextField {
public func cursorPosition(range: NSRange, replacementString string: String, rawNumber: String, formattedNumber: String) -> UITextPosition? {
let start = range.location
let length = string.count
let newCursorLocation = start + length
// Adjust the cursor position to skip over formatting characters
var formattedCharacterCount = 0
for (index, character) in formattedNumber.enumerated() {
if index >= newCursorLocation + formattedCharacterCount {
break
}
if !character.isNumber {
formattedCharacterCount += 1
}
}
let finalCursorLocation = min(newCursorLocation + formattedCharacterCount, formattedNumber.count)
return position(from: beginningOfDocument, offset: finalCursorLocation)
}
}