Skip to content

Instantly share code, notes, and snippets.

@konnnn
Last active April 24, 2019 17:33
Show Gist options
  • Save konnnn/0caf0f08c2c800cf409aecac14f04c18 to your computer and use it in GitHub Desktop.
Save konnnn/0caf0f08c2c800cf409aecac14f04c18 to your computer and use it in GitHub Desktop.
// UITextField+CharactersLimit.swift
// https://gist.github.com/konnnn/0caf0f08c2c800cf409aecac14f04c18
import UIKit
// Limit = 15 characters
extension TextField: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let newLength = text.count + string.count - range.length
return newLength <= 15
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment