Reputation: 1933
I'd like to set a maximum number of characters allowed to be typed both in a UITextView
and a UITextField
. This number will be then shown in a little label (for user's reference, Twitter Style.)
Upvotes: 47
Views: 58307
Reputation: 61
Update for Swift 5
Gives the most forward result
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return range.location < 140 // limit to 140 chars
}
Upvotes: 6
Reputation: 282
Adding support of pasting text with cutting it by a maxLength + avoiding UNDO action crash:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if let maxLength = maxLength {
let maxReplacementLength = min(range.length, maxLength - range.location)
let replacementRange = NSRange(location: range.location, length: maxReplacementLength)
let result = NSString(string: (textView.text ?? "")).replacingCharacters(in: replacementRange, with: text)
if result.count <= maxLength && range.length <= maxLength - range.location {
return true
}
textView.text = String(result[..<result.index(result.startIndex, offsetBy: min(result.count, maxLength))])
return false
}
return true
}
Upvotes: 3
Reputation: 1691
Swift 4 , Xcode 9.1 GM
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return textView.text.count + (text.count - range.length) <= 200
}
Upvotes: 3
Reputation: 21
Swift 5
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var newText = textView.text!
newText.removeAll { (character) -> Bool in
return character == " " || character == "\n"
}
return (newText.count + text.count) <= 40
}
Upvotes: 2
Reputation: 38162
Update Swift 4.X
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
let numberOfChars = newText.count
return numberOfChars < 10 // 10 Limit Value
}
Try this out:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
let numberOfChars = newText.characters.count // for Swift use count(newText)
return numberOfChars < 10;
}
Upvotes: 127
Reputation: 3932
SWIFT 4
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
return newText.count < 10
}
Upvotes: 38
Reputation: 1690
Swift 4/ Xcode 9.1
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let currentText = textView.text ?? ""
guard let stringRange = Range(range, in: currentText) else { return false }
let changedText = currentText.replacingCharacters(in: stringRange, with: text)
return changedText.count <= 399 // Pass your character count here
}
Upvotes: 3
Reputation: 1333
Swift 4:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
return newText.count <= 70
}
Upvotes: 12
Reputation: 843
Swift 3.0
Just override this UITextFieldDelegate function, set the desired characterLimit variable and you are good to go:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
let characterLimit = 5
let newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
let numberOfChars = newText.characters.count
return numberOfChars < characterLimit
}
Upvotes: 8
Reputation: 1750
Try this out:-
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
print("chars \(textView.text.characters.count) \( text)")
if(textView.text.characters.count > 20 && range.length == 0) {
print("Please summarize in 20 characters or less")
return false;
}
return true;
}
Upvotes: 9