ios中實現對UItextField,UITextView等輸入框的字數限制

來源:互聯網
上載者:User

標籤:style   blog   http   strong   os   art   

本文轉載至 http://blog.sina.com.cn/s/blog_9bf272cf01013lsd.html2011-10-05 16:48 533人閱讀 評論(0) 收藏 舉報

1.     如何?對UITextField ,UITextView等輸入框的 字數限制

 

       (1)首先,肯定要 讓controller 實現 UITextFieldDelegate (針對UITextField)或者  UITextViewDelegate(針對UITextView)

 

             然後,將 輸入框的delegate屬性設定為self.  

 

 

      (2) 然後,我們就可以用這兩個delegate的函數來實現 我們對輸入字數的限制了。

 

           對於 UITextField 是函數

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

 

      或者

           對於UITextView 是函數

 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

 

 

      如果允許繼續輸入,那麼返回YES,否則返回NO。

 

     代碼如下:

    

view plain

  1. //如果輸入超過規定的字數100,就不再讓輸入  
  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.  如何? 對有輸入限制的輸入框的剩餘字數的自動計算

 

     比如上面的代碼中,輸入框的字數不能超過100,如何即時的計算出當前可以輸入多少個字元呢?

 

   UITextField 沒有找到合適的函數,也可以在函數

   - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

 

中來實現。

 

   代碼:

  

view plain

  1. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
  2. {  
  3.    int  remainTextNum_=100;  
  4.    //計算剩下多少文字可以輸入  
  5.    if(range.location>=100)  
  6.    {  
  7.       remainTextNum_=0;  
  8.       
  9.       return NO;  
  10.    }  
  11.    else  
  12.    {  
  13.     NSString  * nsTextContent=string.text;  
  14.     int   existTextNum=[nsTextContent length];  
  15.     remainTextNum_=100-existTextNum;  
  16.         return YES;  
  17.    }  
  18.      
  19. }  

 

 

 

 

    UITextView 除了可以在函數

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

    中按照上面類似的辦法處理以外,還可以在函數  

    - (void)textViewDidChange:(UITextView *)textView  中處理。

 

    代碼如下:

    

view plain

  1. //在這個地方計算輸入的字數  
  2. - (void)textViewDidChange:(UITextView *)textView  
  3. {  
  4.     NSString  * nsTextContent=textView.text;  
  5.     int   existTextNum=[nsTextContent length];  
  6.     remainTextNum_=100-existTextNum;  
  7. } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.