如何? 對輸入框的常值內容進行限制的功能

來源:互聯網
上載者:User

如何? 對輸入框的常值內容進行限制的功能

 

  如何?  對輸入框的常值內容進行限制的功能

 

 

 

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

//如果輸入超過規定的字數100,就不再讓輸入  

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

{  

    if (range.location>=100)   

    {  

        return  NO;  

    }  

    else   

    {  

        return YES;  

    }  

}  

 

 

 

 

 

 

2.  如何? 對有輸入限制的輸入框的剩餘字數的自動計算

 

 

 

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

 

 

 

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

 

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

 

 

 

中來實現。

 

 

 

   代碼:

 

  

view plain

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

{  

   int  remainTextNum_=100;  

   //計算剩下多少文字可以輸入  

   if(range.location>=100)  

   {  

      remainTextNum_=0;  

      

      return NO;  

   }  

   else  

   {  

    NSString  * nsTextContent=string.text;  

    int   existTextNum=[nsTextContent length];  

    remainTextNum_=100-existTextNum;  

        return YES;  

   }  

     

}  

 

 

 

 

 

 

 

 

    UITextView 除了可以在函數

 

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

 

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

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

 

 

 

    代碼如下:

 

    

view plain

//在這個地方計算輸入的字數  

- (void)textViewDidChange:(UITextView *)textView  

{  

    NSString  * nsTextContent=textView.text;  

    int   existTextNum=[nsTextContent length];  

    remainTextNum_=100-existTextNum;  

}  

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.