How can I set the maximum amount of characters in a UITextField
on the iPhone SDK when I load up a UIView
?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
While the
UITextField
class has no max length property, it's relatively simple to get this functionality by setting the text field'sdelegate
and implementing the following delegate method:Objective-C
Swift
Before the text field changes, the UITextField asks the delegate if the specified text should 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
NO
to prohibit the change.When typing in a single character at the end of a text field, the
range.location
will be the current field's length, andrange.length
will be 0 because we're not replacing/deleting anything. Inserting into the middle of a text field just means a differentrange.location
, and pasting multiple characters just meansstring
has more than one character in it.Deleting single characters or cutting multiple characters is specified by a
range
with a non-zero length, and an empty string. Replacement is just a range deletion with a non-empty string.A note on the crashing "undo" bug
As is mentioned in the comments, there is a bug with
UITextField
that can lead to a crash.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
UITextField
will attempt to replace the string it thinks it pasted in to itself with an empty string. This will crash because it never actually pasted the string in to itself. It will try to replace a part of the string that doesn't exist.Fortunately you can protect the
UITextField
from killing itself like this. You just need to ensure that the range it proposes to replace does exist within its current string. This is what the initial sanity check above does.swift 3.0 with copy and paste working fine.
Hope it's helpful to you.
I give a supplementary answer based on @Frouo. I think his answer is the most beautiful way. Becuase it's a common control we can reuse.
The following code is similar to sickp's answer but handles correctly copy-paste operations. If you try to paste a text that is longer than the limit, the following code will truncate the text to fit the limit instead of refusing the paste operation completely.
For Xamarin:
Swift 4
Swift 4
Edit: memory leak issue fixed.