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
The best way would be to set up a notification on the text changing. In your
-awakeFromNib
of your view controller method you'll want:Then in the same class add:
Then link up the outlet
myTextField
to yourUITextField
and it will not let you add any more characters after you hit the limit. Be sure to add this to your dealloc method:swift 3.0
This code is working fine when you are paste string more than your character limits.
Thanks for your votes. :)
You can't do this directly -
UITextField
has no maxLength attribute, but you can set theUITextField's
delegate, then use:Using Interface builder you can link and get the event for "Editing changed" in any of your function. Now there you can put check for the length
There is generic solution for setting max length in Swift. By IBInspectable you can add new Attribute in Xcode Attribute Inspector.
The Problem with some of the answer given above is, For example I have a text field and I have to set a limit of 15 characters input, then it stops after entering 15th Character. but they Don't allow to delete. That is the delete button also don't work. As I was facing the same problem. Came out with the solution , Given Below. Works Perfect for Me
I am having a text field, whose tag I have set "6" and I have restricted the max char limit = 30 ; works fine in every case