标签:ios教程 ios uitextfield ios开发 文本框
前面提了按钮,那接下来给大家看一下 UITextField 怎么用。
代码:AppdDelegate.m
//定义变量
UITextField *textField;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIViewController *viewController = [UIViewController alloc];
self.window.rootViewController = viewController;
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController.view = rootView;
/******************** UITextField ********************/
//创建 UITextField
textField = [[UITextField alloc] init];
//设置UITextField的位置,宽高
textField.frame = CGRectMake(100, 100, 200, 30);
/**
* 设置边框样式,有如下枚举值:
* UITextBorderStyleNone(没格式)
* UITextBorderStyleLine(线条)
* UITextBorderStyleBezel(3D)
* UITextBorderStyleRoundedRect(圆角)
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//设置背景颜色
textField.backgroundColor = [UIColor yellowColor];
//设置默认输入文字
//textField.text = @"text";
//设置提示性的文字
textField.placeholder = @"Jss";
//重新编辑会自动清除原有的内容
textField.clearsOnBeginEditing = YES;
//文字的最小值
//textField.minimumFontSize = 10.0f;
//文字自动随着文本框的减小而缩小
textField.adjustsFontSizeToFitWidth = YES;
/**
* 自动转换文本框内输入文本的大小
* UITextAutocapitalizationTypeNone(不切换)
* UITextAutocapitalizationTypeWords(每个单词的首字母)
* UITextAutocapitalizationTypeSentences(每个句子的首字母)
* UITextAutocapitalizationTypeAllCharacters(所有字母)
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
/**
* 设置键盘的类型
* UIKeyboardTypeDefault(默认的键盘)
* UIKeyboardTypeASCIICapable(英文字母键盘)
* UIKeyboardTypeNumbersAndPunctuation(数字和标点符号键盘)
* UIKeyboardTypeURL(URL键盘)
* UIKeyboardTypeNumberPad(数字键盘)
* UIKeyboardTypePhonePad(电话拨号键盘)
* UIKeyboardTypeNamePhonePad(个性电话拨号键盘)
* UIKeyboardTypeEmailAddress(输入E-mail地址的键盘)
* UIKeyboardTypeDecimalPad(可输入数字和小数点的键盘)
* UIKeyboardTypeTwitter(Twitter键盘)
* UIKeyboardTypeWebSearch(网页搜寻键盘)
*/
textField.keyboardType = UIKeyboardTypeNamePhonePad;
/**
* 设置清除按钮类型
* UITextFieldViewModeNever(从不显示清除按钮)
* UITextFieldViewModeWhileEditing(当编辑时显示清除按钮)
* UITextFieldViewModeUnlessEditing(除了编辑外都显示清除按钮)
* UITextFieldViewModeAlways(清除按钮一直出现)
*/
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//添加UITextField到view
[rootView addSubview:textField];
[self.window makeKeyAndVisible];
return YES;
}
/**
* 释放键盘
*/
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[textField resignFirstResponder];
}
运行结果:
值得说的是,最后一段 touchesBegan: 方法。如果没有这一段代码,那么当你在输入框内输入完成之后,键盘是不会自动收回去的。所以,我们要获取当前响应,然后关掉键盘。务必熟记,特别重要。
基础部分,简单介绍一下就行,以后碰到相关的再接着说明其他的使用方法。
第四章,结束!
本篇内容属原创,转载请注明出处,禁止用于商业用途。谢谢!
标签:ios教程 ios uitextfield ios开发 文本框
原文地址:http://blog.csdn.net/zsfz_053/article/details/41805701