UITextField
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#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];
//创建TextField
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 120, 180, 30)];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 120, 40, 30)];
UIView *sssView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
sssView.backgroundColor = [UIColor yellowColor];
//⾃自定义输⼊入视图(默认是键盘)
// textField1.inputView = sssView;
// 输⼊视图上方的辅助视图(默认nil),要添加的辅助视图,可以不给宽度,但是必须要指定高度,宽度默认就是屏幕宽,可以弹广告
textField1.inputAccessoryView = sssView;
nameLabel.text = @"姓名:";
nameLabel.textAlignment = NSTextAlignmentRight;
textField1.text = @"king";
// textField1.placeholder = @"joker"; //用来展位,不是内容
// textField1.font = [UIFont systemFontOfSize:30];
//加粗,字体大小
// textField1.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
textField1.textAlignment = NSTextAlignmentLeft;
//textField1.textColor = [UIColor blueColor];
// 边框
textField1.layer.borderWidth = 1;
textField1.layer.cornerRadius = 5;
//是否在输入的时候清空里面的内容,针对的时text属性的内容,不使placeholder
// textField1.clearsOnBeginEditing = YES; //清空的是text的内容,不是placeholder
textField1.clearsOnBeginEditing = YES;
textField1.enabled = YES;//是否能进行输入
textField1.secureTextEntry = YES; //以密码的方式输入
// textField1.keyboardType = UIKeyboardTypeEmailAddress;//键盘类型
// textField1.returnKeyType = UIReturnKeySearch; //键盘右下⾓角return按钮类型(枚举 值)
//输入框后边的x
textField1.clearButtonMode = UITextFieldViewModeAlways;
//设置代理人
textField1.delegate = self;
textField1.tag = 1000; //window的tag是0
[self.window addSubview:nameLabel];
[self.window addSubview:textField1];
[textField1 release];
[nameLabel release];
// NSLog(@"%p",textField1);
[_window release];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//谁触发的协议方法,对应传过来的textField就是谁
// NSLog(@"%ld", textField.tag);
// NSLog(@"%@", textField.text);
// NSLog(@"%p",textField);
// textField.text = @"aaa";
[textField resignFirstResponder]; //点击return回收键盘
return YES;
}