ios文字框基本使用,以及所有代理方法的作用

來源:互聯網
上載者:User

標籤:

/*

     UITextField文本輸入框

     */

    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 275, 50)];

    //設定邊框形式

    /*

     UITextBorderStyleRoundedRect 圓角形式

     UITextBorderStyleLine 線條形式

     UITextBorderStyleBezel 槽形式

     */

    textField.borderStyle = UITextBorderStyleRoundedRect;

    //通常用於尋找當前文本輸入框中顯示的文字

    textField.text = @"";

    //文本顏色

    textField.textColor = [UIColor redColor];

    //設定文本字型大小

    textField.font = [UIFont systemFontOfSize:20];

    //設定背景顏色

    textField.backgroundColor = [UIColor lightGrayColor];

    //當重複開始編輯時候 清除文字

    textField.clearsOnBeginEditing = YES;

    //文字提示

    textField.placeholder = @"請輸入您的大區名字";

    //文字密文(暗文) 該屬性通常用於設定密碼輸入框

    textField.secureTextEntry = NO;

    //文字輸入時的對齊

    textField.textAlignment = NSTextAlignmentCenter;

    //文字輸入的清除按鈕

    /*

     UITextFieldViewModeWhileEditing 當輸入時

     UITextFieldViewModeAlways 總是

     UITextFieldViewModeUnlessEditing 不在輸入時候

     */

    textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    //鍵盤的類型

    textField.keyboardType = UIKeyboardTypeDefault;

    //retuan鍵類型 可自訂鍵盤

    textField.returnKeyType = UIReturnKeyJoin;

    //左視圖

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    label.text = @"帳號";

    label.textAlignment = NSTextAlignmentCenter;

    

    textField.leftView = label;

    

    textField.leftViewMode = UITextFieldViewModeWhileEditing;

    

    //右視圖

    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

    [button setTitle:@"確定" forState:UIControlStateNormal];

    

    button.frame = CGRectMake(0, 0, 50, 50);

    

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    

    textField.rightView = button;

    

    textField.rightViewMode = UITextFieldViewModeAlways;

    

    [self.window addSubview:textField];

    

    //讓鍵盤產生第一響應 鍵盤會自動彈起

    [textField becomeFirstResponder];

    //收合鍵盤

    /*

     1、點擊鍵盤的return鍵

     2、點擊Button

     3、點擊空白處彈回鍵盤

     */

  

/*

     手勢

     */

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];

 

 

自定製方法/手勢方法

- (void)tapClick{

    UITextField * textField = (UITextField*)[self.window viewWithTag:100];

    

    [textField resignFirstResponder];

}

 

- (void)buttonClick:(UIButton*)button{

    //取消第一響應

    UITextField * textFiled = (UITextField*)[self.window viewWithTag:100];

    

    [textFiled resignFirstResponder];

}

 

 

 

所有代理方法作用

 

//當Return鍵被點擊時調用 通常用於收回鍵盤

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    

    [textField resignFirstResponder];

    

    return YES;//5.1前設定NO為點擊無效

}

//文本輸入框開始輸入時調用

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    //將鍵盤彈出

    NSLog(@"開始輸入");

}

//文本輸入框結束輸入時調用

- (void)textFieldDidEndEditing:(UITextField *)textField{

    //擷取當前文本輸入框中所輸入的文字

    NSLog(@"所輸入的內容為:%@",textField.text);

    //例:判斷帳號書寫形式是否正確 如果不正確提示填寫錯誤 重新輸入

    

    NSLog(@"結束輸入");

}

//文本輸入框內容發生變化即會調用的方法

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

    /*

    NSLog(@"內容:%@",textField.text);//擷取的是上一次所輸入內容

 

    NSLog(@"Location:%lu Length:%lu",range.location,range.length);//範圍為當前文字的位置,長度為零

    

    NSLog(@"==%@==",string);//即時擷取當前輸入的字元

    */

    //需求 即時擷取當前文字框中的所有文字

    NSString * resultStr = [textField.text stringByAppendingString:string];

    

    NSLog(@"%@",resultStr);

    

    //可在該方法中判斷所輸入文字是否正確

    

    return YES;

}

//瞭解

//是否允許文本輸入框可以輸入

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    return YES;

}

//是否允許文本輸入框結束

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    //在該方法中可以通過判斷文本長度限制鍵盤是否可以收回

    return NO;

}

//是否允許被清除

- (BOOL)textFieldShouldClear:(UITextField *)textField{

    NSLog(@"文字被清除");

    return YES;

}

 

ios文字框基本使用,以及所有代理方法的作用

聯繫我們

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