Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While the <code>UITextField</code> class has no max length property, it's relatively simple to get this functionality by setting the text field's <code>delegate</code> and implementing the following delegate method:</p> <p><strong>Objective-C</strong></p> <pre><code>- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // Prevent crashing undo bug – see note below. if(range.length + range.location &gt; textField.text.length) { return NO; } NSUInteger newLength = [textField.text length] + [string length] - range.length; return newLength &lt;= 25; } </code></pre> <p><strong>Swift</strong></p> <pre><code>func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool { let currentCharacterCount = textField.text?.characters.count ?? 0 if (range.length + range.location &gt; currentCharacterCount){ return false } let newLength = currentCharacterCount + string.characters.count - range.length return newLength &lt;= 25 } </code></pre> <p>Before the text field changes, the UITextField asks the delegate if the specified text <em>should</em> be changed. The text field has not changed at this point, so we grab it's current length and the string length we're inserting (either through pasting copied text or typing a single character using the keyboard), minus the range length. If this value is too long (more than 25 characters in this example), return <code>NO</code> to prohibit the change.</p> <p>When typing in a single character at the end of a text field, the <code>range.location</code> will be the current field's length, and <code>range.length</code> will be 0 because we're not replacing/deleting anything. Inserting into the middle of a text field just means a different <code>range.location</code>, and pasting multiple characters just means <code>string</code> has more than one character in it.</p> <p>Deleting single characters or cutting multiple characters is specified by a <code>range</code> with a non-zero length, and an empty string. Replacement is just a range deletion with a non-empty string.</p> <h1>A note on the crashing "undo" bug</h1> <p>As is mentioned in the comments, there is a bug with <code>UITextField</code> that can lead to a crash.</p> <p>If you paste in to the field, but the paste is prevented by your validation implementation, the paste operation is still recorded in the application's undo buffer. If you then fire an undo (by shaking the device and confirming an Undo), the <code>UITextField</code> will attempt to replace the string it <em>thinks</em> it pasted in to itself with an empty string. This will crash because it never <em>actually</em> pasted the string in to itself. It will try to replace a part of the string that doesn't exist.</p> <p>Fortunately you can protect the <code>UITextField</code> from killing itself like this. You just need to ensure that the range it proposes to replace <em>does</em> exist within its current string. This is what the initial sanity check above does.</p> <h1>swift 3.0 with copy and paste working fine.</h1> <pre><code>func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool { let str = (textView.text + text) if str.characters.count &lt;= 10 { return true } textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10)) return false } </code></pre> <p>Hope it's helpful to you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload