To restrict user input (e.g., character limits, specific character types) in UITextField or UITextView, use the inputRestrictions method.
Important for Paste Handling:
If you need to process pasted content (e.g., to strip newlines or enforce limits on pasted text), you must subclass the standard components:
- Use
JKPastedTextField instead of UITextField. - Use
JKPastedTextView instead of UITextView.
After subclassing, set isRemovePasteboardNewlineCharacters to enable effective paste processing.
// Example of subclassing for paste support
class MyTextField: JKPastedTextField {
override init(frame: CGRect) {
super.init(frame: frame)
self.isRemovePasteboardNewlineCharacters = true
self.inputRestrictions(...) // Apply restrictions
}
}