首页 > 代码库 > IOS代码布局(三) UITextField
IOS代码布局(三) UITextField
(一)常规操作
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。