Skip to content

Instantly share code, notes, and snippets.

View joeboyscout04's full-sized avatar

Joe Elliott joeboyscout04

View GitHub Profile
@Blackjacx
Blackjacx / UITextField-Replace-TextTheRightWay.swift
Last active December 28, 2023 17:18
UITextField - Replace Text The Right Way
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let strippedString = <change replacements string so it fits your requirement - strip, trim, etc>
// replace current content with stripped content
if let replaceStart = textField.position(from: textField.beginningOfDocument, offset: range.location),
let replaceEnd = textField.position(from: replaceStart, offset: range.length),
let textRange = textField.textRange(from: replaceStart, to: replaceEnd) {
textField.replace(textRange, withText: strippedString)