// Copyright © 2016年 你国哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
UITextField *field;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
UITextField 文本输入框
*/
//1.创建
field = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
//2.显示
[self.view addSubview:field];
//3.属性
/*
继承自UIView的属性: tag backgroundColor hidden userInteractionEnabled
边框样式:
UITextBorderStyleNone, 默认->无边框
UITextBorderStyleLine, 边线
UITextBorderStyleBezel, 立体阴影
UITextBorderStyleRoundedRect 圆角矩形
*/
field.borderStyle = UITextBorderStyleRoundedRect;
//设置输入文本的字号
field.font = [UIFont systemFontOfSize:26];
//设置文本的颜色
field.textColor = [UIColor greenColor];
//设置对齐的方式
field.textAlignment = NSTextAlignmentCenter;
//设置文本内容
field.text = @"设置文本内容";

屏幕快照 2016-02-26 上午10.24.51.png
/*
设置输入文本的大写方式
UITextAutocapitalizationTypeNone,系统默认
UITextAutocapitalizationTypeWords,单词首字母大写
UITextAutocapitalizationTypeSentences,句子首字母大写
UITextAutocapitalizationTypeAllCharacters,所有字母大写
*/
// field.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//输入框为空是的提示文本
field.placeholder = @"请输入账号";

屏幕快照 2016-02-26 上午10.29.11.png
//开启安全输入
field.secureTextEntry = YES;

屏幕快照 2016-02-26 上午10.30.51.png
/*
输入框的清除按钮:输入框有内容的情况
UITextFieldViewModeNever, 永远不显示x
UITextFieldViewModeWhileEditing, 编辑时显示x
UITextFieldViewModeUnlessEditing, 非编辑时显示x
UITextFieldViewModeAlways 永远显示x
*/
field.clearButtonMode = UITextFieldViewModeUnlessEditing;
//第一响应者
/*
KeyBoard 键盘
*/
/*
UIReturnType: return键的样式
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue
*/
field.returnKeyType = UIReturnKeySend;
/*
键盘类型
UIKeyboardTypeDefault,系统默认
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad, 输入数字时的键盘
UIKeyboardTypePhonePad, 打电话时的键盘
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeDecimalPad
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
*/
field.keyboardType = UIKeyboardAppearanceDefault;
//代理
field.delegate = self;
}
- (IBAction)tap:(UIButton *)sender {
//成为第一相应者
// [field becomeFirstResponder];
//失去第一响应者
[field resignFirstResponder];
}
/*———————————————————————————代理设计模式————————————————————————————————————*/
//A中定义协议c 添加一个属性di<c>
//B成为A的delegate B实现<c>的方法
#pragma mark --UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"将要开始编辑时");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"已经开始编辑时");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"将要结束编辑时");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"已经结束编辑时");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"修改文本内容");
NSLog(@"%@",NSStringFromRange(range));
NSLog(@"%@",string);
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"清空");
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"发送了内容:%@",field.text);
return YES;
}
@end
border //边
roundedrect //圆角矩形
sentences //句子
mark //记号
delegate //代表
range //范围
clear //清空
placeholder //占位符