IOS代碼布局(三) UITextField

來源:互聯網
上載者:User

標籤:

(一)常規操作

  1.定義一個UITextField,名為textField:

UITextField *textField = = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 40)];

   2.設定背景顏色

textField.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:0.5];

  3.??????設定邊框類型

textField.borderStyle = UITextBorderStyleRoundedRect; 

  4.設定背景圖片

textField???.background = [UIImage imageNamed:@""];

  5.設定委派物件

textField.delegate = self;                                                   

  6.設定預設提示文字

textField.placeholder = @"請輸入文字";

  7.彈出鍵盤(變成第一響應者)

[textField becomeFirstResponder];

  8.文字內容 垂直置中

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

 

(二)委託設定

  1.在聲明中引入委託名( ViewController.m中)

@interface ViewController ()<UITextFieldDelegate/*這裡引入UITextField 需要的委託*/>{    //在這裡聲明全域 私人變數    UITextField *textField;}

  2.設定委派物件

textField.delegate = self;  

  3.實現委託方法

//以下是UITextFieldDelegate 的部分委託實現//*******************************************************************************#pragma mark -------------------#pragma mark UITextFieldDelegate- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    // return NO to disallow editing.    return YES;}- (void)textFieldDidBeginEditing:(UITextField *)textField{    // became first responder    // 在這裡監聽UITextField becomeFirstResponder事件}- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end    return YES;}- (void)textFieldDidEndEditing:(UITextField *)textField{    // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called    // 在這裡監聽UITextField resignFirstResponder事件}- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    // return NO to not change text    NSLog(@"inputText: %@", textField.text);    return 

 

IOS代碼布局(三) UITextField

聯繫我們

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