UITextField
import “AppDelegate.h”
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc{
[_window release];
[super dealloc];
}
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];// 输入框
UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
textField.backgroundColor=[UIColor whiteColor];
[self.window addSubview:textField];
[textField release];
// 边框
textField.layer.borderWidth=1;
// 圆角
textField.layer.cornerRadius=10;// 实现文本内容
textField.text=@”测试一下”;
// 文本颜色
textField.textColor=[UIColor redColor];
// 文本对齐方式
textField.textAlignment=NSTextAlignmentCenter;
// 文本字体
textField.font=[UIFont systemFontOfSize:20];// 占位文本
textField.placeholder=@”请输入账号”;// 输入密码的时候会把文本变成圆点
textField.secureTextEntry=YES;// 设置不同的键盘类型
textField.keyboardType=UIKeyboardTypeNumberPad;// 改变return的样式
// 可以把return按钮切换成不同的样式
textField.returnKeyType=UIReturnKeySearch;// 清除上一个写的内容
textField.clearsOnBeginEditing=YES;// 清除按钮的样式
textField.clearButtonMode=UITextFieldViewModeAlways;// 创建一个view
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
view.backgroundColor=[UIColor redColor];// 弹出一个自定义的视图,默认是键盘
textField.inputView=view;// 给键盘添加一个辅助视图
textField.inputAccessoryView=view;// 点击return回收键盘
在.h中签订textField协议
// 给textField设置代理人
textField.delegate=self;return YES;
}
// 实现协议方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@”测试return按钮”);
// 这句话是实现回收键盘的关键
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@”测试清除按钮”);
return YES;
}