iOS UITextField限制輸入長度,iosuitextfield

來源:互聯網
上載者:User

iOS UITextField限制輸入長度,iosuitextfield

這篇部落客要講限制輸入長度的問題,前幾天有人問我這個問題,說限制長度會出現無法刪除問題,於是正好一塊發出來給大家看看。textField的縮排,一張背景圖片搞定的事,我這裡用了leftView純屬附帶。

好了廢話少說,貼代碼,很簡單,大家一看便知。

//先建立一個textField 和 一個button。

#import "ViewController.h"@interface ViewController ()<UITextFieldDelegate> {        UITextField *currentTextFeild;    UIButton    *touchButton;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        UITextField *textFields = [[UITextField alloc] initWithFrame:CGRectMake(15, 50, self.view.bounds.size.width-15*2, 40)];    textFields.backgroundColor = [UIColor brownColor];    textFields.layer.cornerRadius = 5;    textFields.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];    textFields.leftViewMode = UITextFieldViewModeAlways;//這兩行是為了不讓Text太貼textField的左邊    textFields.placeholder = @"請輸入手機號";    textFields.delegate = self;    [self.view addSubview:textFields];    currentTextFeild = textFields;        UIButton *enableButton = [UIButton buttonWithType:UIButtonTypeCustom];    enableButton.frame = CGRectMake(15, 100, self.view.bounds.size.width-15*2, 40);    enableButton.layer.cornerRadius = 5;    enableButton.backgroundColor = [UIColor grayColor];    [enableButton setTitle:@"沒內容不可點擊" forState:UIControlStateNormal];    [enableButton setTitle:@"可以按了" forState:UIControlStateSelected];    [enableButton setTitle:@"按下去了" forState:UIControlStateHighlighted];    enableButton.enabled = NO;    [enableButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:enableButton];    touchButton = enableButton;}- (void)btnClick {        }

//設定textField代理 

#pragma mark -  UITextFieldDelegate- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {        return YES;}- (void)textFieldDidEndEditing:(UITextField *)textField {        }- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {        //用來判斷是否可以繼續輸入, - range.length是為了判斷是否可以刪除    NSInteger currentLength = textField.text.length - range.length + string.length;    if (currentLength > 11) {                return NO;    }        //判斷按鈕是否可以enable = YES    if (currentTextFeild.text && currentTextFeild.text.length > 0 && currentLength > 0) {                touchButton.enabled = YES;        touchButton.selected = YES;    }else {                touchButton.enabled = NO;        touchButton.selected = NO;    }        if (currentLength <= 0) {                touchButton.enabled = NO;        touchButton.selected = NO;    }        return YES;}- (BOOL)textFieldShouldClear:(UITextField *)textField {        if (currentTextFeild.tag == 11 || currentTextFeild.tag == 12) {        //手機號        touchButton.enabled = NO;        touchButton.selected = NO;;    }        return YES;}- (BOOL)textFieldShouldReturn:(UITextField *)textField {        [textField resignFirstResponder];        return YES;}

 大家看了代碼,差不多就明白了。

 

相關文章

聯繫我們

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