<pre name="code" class="objc">ViewController遵守<span style="font-family: Arial, Helvetica, sans-serif;">UITextFieldDelegate协议</span>
//
// ViewController.h
// IOS150702_UI(02)_UITextField
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@end
//
// ViewController.m
// IOS150702_UI(02)_UITextField
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width-40, 50)];
textField.backgroundColor = [UIColor greenColor];
//设置文本框边框的风格
// typedef NS_ENUM(NSInteger, UITextBorderStyle) {
// UITextBorderStyleNone,
// UITextBorderStyleLine,
// UITextBorderStyleBezel,
// UITextBorderStyleRoundedRect
// };
textField.borderStyle = UITextBorderStyleRoundedRect;//圆角
//设置默认提示文本,当编辑内容时,提示内容消失
textField.placeholder = @"请输入内容";
//设置文本框的初始内容
textField.text = @"你好中国";
//设置文本颜色
textField.textColor = [UIColor redColor];
//设置文本内容字体
textField.font = [UIFont systemFontOfSize:25];
//设置文本的对齐方式
textField.textAlignment = NSTextAlignmentRight;
//设置文本编辑时,是否清空文本内容
textField.clearsOnBeginEditing = YES;
//设置何时显示清除按钮
// typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
// UITextFieldViewModeNever,
// UITextFieldViewModeWhileEditing,
// UITextFieldViewModeUnlessEditing,
// UITextFieldViewModeAlways
// };
textField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑是显示清除按钮
//设置左视图
//设置IMageView的origin无效
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"player2"]];
imageView.frame = CGRectMake(0, 0, 30, 30);
textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = imageView;
//设置右视图
UIImageView *rightimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"player2"]];
rightimageView.frame = CGRectMake(0, 0, 30, 30);
textField.rightViewMode = UITextFieldViewModeAlways;
textField.rightView = rightimageView;
//设置垂直方向的文字对齐方式
//typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
// UIControlContentVerticalAlignmentCenter = 0,
// UIControlContentVerticalAlignmentTop = 1,
// UIControlContentVerticalAlignmentBottom = 2,
// UIControlContentVerticalAlignmentFill = 3,
//};
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
//设置水平方向的对齐方式
//typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
// UIControlContentHorizontalAlignmentCenter = 0,
// UIControlContentHorizontalAlignmentLeft = 1,
// UIControlContentHorizontalAlignmentRight = 2,
// UIControlContentHorizontalAlignmentFill = 3,
//};
textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//在模拟器上切换键盘 command + K
//shift + command + k 开启电脑键盘
//设置键盘的风格
//设置键盘风格
//UIKeyboardTypeDefault, 默认键盘,支持所有字符
//UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
//UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
//UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
//UIKeyboardTypeNumberPad, 数字键盘
//UIKeyboardTypePhonePad, 电话键盘
//UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
//UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
//UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
//UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
//UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
textField.keyboardType = UIKeyboardTypeURL;
//设置键盘return键风格
//UIReturnKeyDefault,
//UIReturnKeyGo,
//UIReturnKeyGoogle,
//UIReturnKeyJoin,
//UIReturnKeyNext,
//UIReturnKeyRoute,
//UIReturnKeySearch,
//UIReturnKeySend,
//UIReturnKeyYahoo,
//UIReturnKeyDone,
//UIReturnKeyEmergencyCall,
textField.returnKeyType = UIReturnKeyGo;
UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, self.view.frame.size.width-40, 50)];
secondTextField.backgroundColor = [UIColor yellowColor];
secondTextField.borderStyle = UITextBorderStyleRoundedRect;
//设置密码风格输入
secondTextField.secureTextEntry = YES;
//设置该UITextField为第一窗口响应者
[secondTextField becomeFirstResponder];
//设置ViewController作为TextField的代理,ViewController要遵循UITextFieldDelegate协议
secondTextField.delegate = self;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width-40, 50)];
label.backgroundColor = [UIColor grayColor];
label.tag = 101;
[self.view addSubview:label];
[self.view addSubview:secondTextField];
[self.view addSubview:textField];
}
//给代码做一个标签,方便我们找到该段代码
#pragma mark UITextFieldDelegate
//
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"将要开始编辑");
return YES; //返回NO则不能进行后续方法的调用,不会调用textFieldDidBeginEditing
}
//开始编辑文本框的时候调用这个方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"开始编辑");
}
// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"将要结束");
return YES;
}
// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"结束编辑");
UILabel *label = (UILabel *)[self.view viewWithTag:101];
label.text = textField.text;
}
// called when 'return' key pressed. return NO to ignore.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"returnKey被点击");
[textField resignFirstResponder]; //隐藏键盘
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
结果图: