Only a few lines of IOS code limit textfield input Length _ios

Source: Internet
Author: User

There are a lot of restrictions on the Internet TextField input length method, but I do not think it is perfect, accurate to say is not in line with the actual development requirements, so here to tidy up the TextField limit the length of the input method.

What I'm using is not the listening method but the most different proxy implementations, why not use listening???
When you see this article is likely to be a matter of distress that is the use of listening to limit the length of input can not be perfect control of the input content.

To give a simple example:

You want to limit the input length to 30 characters, and when you enter 30 characters after the monitor can really good control does not allow you to continue typing, but the problem comes along, when you move the cursor to the middle of the input, you can continue typing this input is very annoying because when you enter your cursor will move to the end, and limit your continued input, but the inner pride you just entered remains in the middle of the text, which is very inconsistent with the requirements.

So using proxies here can be a good implementation of the results we want, just a few lines of code that would like to help you.

 -(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) String {
  
  if (TextField = = Self.livethemetextfield) {
  //if time here in order to get the delete operation, If there is no secondary if will cause the deletion of the key after the word limit can not use the consequences.
    if (range.length = = 1 && string.length = 0) {return
      YES;
    }
    So easy
    else if (self.liveThemeTextField.text.length >=) {
      Self.liveThemeTextField.text = [ Textfield.text substringtoindex:30];
      return NO;
    }
  Return YES
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.