The word limit for input boxes such as Uitextfield,uitextview is implemented in iOS

Source: Internet
Author: User

this article reprinted to http://blog.sina.com.cn/s/blog_9bf272cf01013lsd.html2011-10-05 16:48 533 people Read reviews (0)CollectionReport

1. How to implement the word limit for Uitextfield, uitextview and other input boxes

(1) First, be sure to let controller implement Uitextfielddelegate (for Uitextfield) or uitextviewdelegate (for Uitextview)

Then, set the delegate property of the input box to self.

(2) Then, we can use these two delegate functions to achieve our limit on the number of input words.

For Uitextfield is a function

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string;

Or

For Uitextview is a function

-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text;

If you are allowed to continue typing, return yes, otherwise return No.

The code is as follows:

View Plain

    1. If the input exceeds the specified word count of 100, the input is no longer allowed
    2. -(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString * ) text
    3. {
    4. if (range.location>=100)
    5. {
    6. return NO;
    7. }
    8. Else
    9. {
    10. return YES;
    11. }
    12. }

2. How to implement automatic calculation of the remaining words of the input box with input limit

For example, the above code, the input box can not exceed 100 words, how to calculate in real-time how many characters can be entered at the moment?

Uitextfield does not find a suitable function, it can also be used in the function

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string;

To achieve.

Code:

View Plain

  1. -(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring :(NSString *) string
  2. {
  3. int remaintextnum_=100;
  4. Calculate how much text is left to enter
  5. if (range.location>=100)
  6. {
  7. remaintextnum_=0;
  8. return NO;
  9. }
  10. Else
  11. {
  12. NSString * NSTEXTCONTENT=STRING.TEXT;
  13. int existtextnum=[nstextcontent length];
  14. Remaintextnum_=100-existtextnum;
  15. return YES;
  16. }
  17. }

Uitextview In addition to the function

-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text

is handled in a similar way as above, you can also

-(void) Textviewdidchange: (Uitextview *) processed in TextView.

The code is as follows:

View Plain

    1. //Calculate the number of words entered in this place   
    2. -  ( void ) Textviewdidchange: (uitextview *) textview  
    3. {  
    4.     NSString  *  nstextcontent=textview.text;  
    5.      int     existtextnum=[nstextcontent length];  
    6.     remaintextnum_=100 -existtextnum;  
    7. } 

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.