UI基础:UITextField 分类: iOS学习-UI 2015-07-01 21:07 68人阅读 评论(0) 收藏

UITextField 继承自UIControl,他是在UILabel基础上,对了文本的编辑.可以允许用户输入和编辑文本
UITextField的使用步骤
1.创建控件

UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(10, 100, 300, 50)];
textField.tag=100;//为textField赋tag,目的是可以通过父视图找到textField

2.设置属性
(1).设置背景

textField.backgroundColor=[UIColor yellowColor];

(2).设置文本框提示文字

textField.placeholder=@"请输入金额";//输入占位符.一旦有内容输入就消失

(3).设置文本

textField.text=@"520$";

(4).设置文本颜色

textField.textColor=[UIColor redColor];

(5).文本对齐方式

textField.textAlignment=NSTextAlignmentCenter;//居中

(6).设置文本框的样式

textField.borderStyle=UITextBorderStyleRoundedRect;//文本框边角圆弧

(7).设置文本框是否能被编辑(默认是YES,可以编辑)

textField.enabled=NO;

(8).当开始输入文本时,是否清空原文本框的内容(默认值是NO)

textField.clearsOnBeginEditing=YES;

(9)设置return的按键样式

textField.returnKeyType=UIReturnKeyGo;

(10)设置弹出键盘的样式

textField.keyboardType=UIKeyboardTypeNumberPad;//弹出数字键盘

(11)设置输入文本框的文字以密码模式显示

textField.secureTextEntry=YES;

(12)自定义弹出视图

UIView *inPutView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 250)];
nPutView.backgroundColor=[UIColor cyanColor];
textField.inputView=inPutView;
[inPutView release];

(13)自定义键盘上方的辅助视图

UIView *accessView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
accessView.backgroundColor=[UIColor cyanColor];
textField.inputAccessoryView=accessView;
[accessView release];

(14)设置文本框的清理模式

textField.clearButtonMode=UITextFieldViewModeWhileEditing;//当编辑时显示清理按钮

(15)设置文本框的代理
针对某个类的代理(协议)的命名规则:类名+delegate
当一个类的属性遵循了某个协议的属性.它的命名:delegate
delegate的属性语义特性使用assign
在对象(-)方法里,self代表该对象,在类(+)方法,self代表该类

textField.delegate=self;

//总结:要遵循代理(协议),这个类必须是已知类(因为要在遵循代理的类的.m中实现协议方法)
// 添加到父视图

[self.window addSubview:textField];

// 释放所有权

[textField release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark -----------UITextFieldDelegate代理方法--------------
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//回收键盘
    [textField resignFirstResponder];
    return YES;
}
#pragma mark --------不是代理的方法,是点击事件的方法----------
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITextView *textField=(UITextView *)[self.window viewWithTag:100];//通过tag找到textField
    [textField resignFirstResponder]; //回收键盘
}

文本显示

属性名 描述 示例
text 要显示的⽂本内容 textField.text = @“fuck”;
textColor 文本内容的颜色 textField.textColor = [UIColor redColor];
textAlignment 文本的对齐方式(水平方向) textField.textAlignment = NSTextAlignmentLeft;
font 文本字体 textField.font = [UIFont fontWithName:@“Helvetica- Bold” size:20];//黑体加粗,20号字。
placeholder 占位字符串,即没输入时,给出提示字符串 textField.placeholder = @“请输入⽤用户名”;

输入控制
这里写图片描述
外观控制
这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/shaoting/p/4619809.html