6 June 2024 Leave a comment Tech-Help
When developing iOS applications, you might encounter a requirement to limit the number of characters a user can input into a UITextField. Unfortunately, the UITextField class does not provide a built-in property for setting a maximum character length. However, you can achieve this functionality by leveraging the UITextFieldDelegate protocol.
Implementing Character Limit in Objective-C
To set a maximum character length in a UITextField, you need to implement the textField:shouldChangeCharactersInRange:replacementString:
delegate method. Here’s how you can do it:
Objective-C Code Example
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Prevent crashing undo bug
if(range.length + range.location > textField.text.length) {
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
In this method, before the text field changes, we calculate the new length by adding the length of the current text and the length of the new string, then subtracting the range length. If the new length exceeds the limit (25 in this example), we return NO
to prevent the change.
Implementing Character Limit in Swift
Similarly, in Swift, you can achieve this by implementing the textField(_:shouldChangeCharactersIn:replacementString:)
method:
Swift Code Example
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.count ?? 0
if range.length + range.location > currentCharacterCount {
return false
}
let newLength = currentCharacterCount + string.count - range.length
return newLength <= 25
}
This Swift implementation follows the same logic as the Objective-C example to ensure the text length does not exceed the specified limit.
Advanced Implementation with Copy and Paste Handling
If you want to handle scenarios where users might paste text into the UITextField, you can use the following Swift implementation that ensures the pasted text is truncated to fit within the character limit:
Swift Code Example for Copy-Paste Handling
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let str = (textView.text + text)
if str.count <= 10 {
return true
}
textView.text = String(str.prefix(10))
return false
}
This method ensures that if the user pastes text longer than the limit, the text is truncated to fit within the allowed character count.
Why Use Repeato for Your Testing Needs?
As a mobile developer, managing and maintaining test cases can be time-consuming, especially when dealing with UI elements like UITextField. This is where Repeato can significantly streamline your workflow. Repeato is a No-code test automation tool for iOS and Android, designed to create, run, and maintain automated tests for your apps efficiently.
Repeato utilizes computer vision and AI, making it particularly fast to edit and run tests. It allows developers to focus on creating a great product, while also enabling non-technical colleagues or QA teams to handle test automation. This way, you can ensure your UITextField character limits and other functionalities are thoroughly tested without extensive manual effort.
For more information on how Repeato can help you, check out our documentation or contact us for a demo.