UITextField
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITextField相关的知识,希望对你有一定的参考价值。
#import "AppDelegate.h"
//extension
@interface AppDelegate ()<UITextFieldDelegate>
@end
@implementation AppDelegate
- (void)dealloc
{
self.window = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[UIViewController alloc] init];
//
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 300, 50)];
/*
UITextBorderStyleLine 黑色边框
UITextBorderStyleRoundedRect 圆角边框
*/
//设置边框样式
textField.borderStyle = UITextBorderStyleRoundedRect;
//设置/获取文本内容
// textField.text = @"I‘m a text";
//提示文本
textField.placeholder = @"请输入密码";
/*
UITextFieldViewModeWhileEditing 编辑时出现
UITextFieldViewModeAlways
*/
//设置清除按钮
textField.clearButtonMode = UITextFieldViewModeAlways;
/*
UITextAutocapitalizationTypeSentences 默认
UITextAutocapitalizationTypeNone 开头字母小写
*/
//设置开头字母大小写
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
/*
UIKeyboardTypeNumbersAndPunctuation 数字加标点
UIKeyboardTypeNumberPad 纯数字
UIKeyboardTypeURL url
*/
//设置键盘
textField.keyboardType = UIKeyboardTypeURL;
//设置return键
textField.returnKeyType = UIReturnKeySearch;
//是否密文文本
textField.secureTextEntry = YES;
//设置代理
textField.delegate = self;
[self.window addSubview:textField];
[textField release];
//让textField成为第一响应者
//[textField becomeFirstResponder];
return YES;
}
#pragma mark - UITextFieldDelegate
//是否可以开始编辑 yes 才可以编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"%s", __func__);
return YES;
}
//开始进入编辑状态
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%s", __func__);
}
//是否可以结束编辑 yes 可以结束
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"%s", __func__);
if (textField.text.length < 6)
{
//警告视图创建
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"输入密码不足6位" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", @"ok1", @"ok2", nil];
[alertView show];
[alertView release];
return NO;
}
return YES;
}
//进入结束编辑的状态
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"%s", __func__);
}
//点击return按钮的时候
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%s", __func__);
//取消第一响应者, 会回收键盘
[textField resignFirstResponder];
return YES;
}
@end
以上是关于UITextField的主要内容,如果未能解决你的问题,请参考以下文章