UITextField用法大全
2015-08-14 14:48
381 查看
#import “AppDelegate.h”
@interface AppDelegate ()
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UITextField *textField2;
@end
@implementation AppDelegate
(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];
//输入框中一开始就有的文字
//属性文本, 分配一个新值给这个属性, 用同样的字符串数据取代这个文本属性
//当输入框没有内容时,水印提示 (默认使用70%灰色)
//当输入框没有内容时, 分配一个新值给这个属性, 用同样的字符串数据取代这个占位符属性
//默认情况下, 该属性返回值为一个文本属性的字典,
//设置输入框内容的字体样式和大小
//设置字体颜色
//设置输入框的背景颜色
//文本对齐方式
//类型属性, 新的文本会被用户输入, 如果textField不是编辑模式, 属性值为空, 同样, 你不能向属性赋值除非textField是编辑模式
//如果你设置为YES, 你也需要通过minimumFontSize属性来设置最小字体大小
//设置自动缩小显示的最小字体大小
//是否插入文本取代先前内容, 默认为NO
//是否允许编辑文本属性, 默认为NO
//设置背景图片,如果使用了自定义的背景图片边框会被忽略掉
//设置背景图片, 如果background属性没有被设置, 此属性会被忽略
//左视图模式, 用于在何时出现该视图, 与清除文本模式相同
//设置左视图, 不要忘了打开视图模式
//右视图模式, 用于在何时出现该视图, 与清除文本模式相同
// inputAccessoryView: 编辑时显示在系统键盘或用户自定义的inputView上面的视图
//输入安全属性, 默认为NO
//自动更正功能
//首字母是否大写
//设置return键
//设置键盘外观
/* 代理详情请看 “#pragma mark * 访问代理” /
/* 创建另一个对象 */
}
/* 文本框应该开始编辑 BOOL值 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// return NO; /* 如果返回值为NO, 相当于禁止编辑 */
return YES;
}
/* 文本框已经开始编辑 */
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
/* 文本框应该结束编辑 BOOL值 */
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
/* 文本框已经结束编辑 */
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
//编辑文本
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
//这对于想要加入撤销选项的应用程序特别有用
//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
//要防止文字被改变可以返回NO
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
return YES;
}
/* 是否允许根据用户请求清除内容, 可以设置在特定条件下才允许清除内容 */
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.textField == textField) {
[self.textField2 becomeFirstResponder]; /* return键或回车键, 自动跳到下一个输入文本框 */
} else if (self.textField2 == textField)
{
[self.textField2 resignFirstResponder]; /* 按return键或回车键, 键盘往下收 becomeFirstResponder */
}
return YES;
}
/* 点击空白区域回收键盘 */
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
[self.window endEditing:YES];
}
@interface AppDelegate ()
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UITextField *textField2;
@end
@implementation AppDelegate
(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];
pragma mark - 文字属性
//创建textfield并设置位置及大小self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; [self.window addSubview:self.textField];
//输入框中一开始就有的文字
self.textField.text = @"";
//属性文本, 分配一个新值给这个属性, 用同样的字符串数据取代这个文本属性
self.textField.attributedText = [[NSAttributedString alloc] initWithString:@""];
//当输入框没有内容时,水印提示 (默认使用70%灰色)
self.textField.placeholder = @"WELCOME";
//当输入框没有内容时, 分配一个新值给这个属性, 用同样的字符串数据取代这个占位符属性
//textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@""];
//默认情况下, 该属性返回值为一个文本属性的字典,
self.textField.defaultTextAttributes = @{};
//设置输入框内容的字体样式和大小
self.textField.font = [UIFont fontWithName:@"黑体" size:20]; //textField.font = [UIFont systemFontOfSize:20];
//设置字体颜色
self.textField.textColor = [UIColor redColor];
//设置输入框的背景颜色
self.textField.backgroundColor = [UIColor whiteColor];
//文本对齐方式
self.textField.textAlignment = 1; /* enum { NSTextAlignmentLeft = 0, 文本向左对齐 NSTextAlignmentCenter = 1, 文本居中 NSTextAlignmentRight = 2, 文本向右对齐 NSTextAlignmentJustified = 3, 调整文本,以便在段落的最后一行是自然对齐。 NSTextAlignmentNatural = 4, 使用与当前脚本关联的默认对齐方式。 }; typedef NSInteger NSTextAlignment; */
//类型属性, 新的文本会被用户输入, 如果textField不是编辑模式, 属性值为空, 同样, 你不能向属性赋值除非textField是编辑模式
self.textField.typingAttributes = @{};
pragma mark - 文字大小
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动,//如果你设置为YES, 你也需要通过minimumFontSize属性来设置最小字体大小
self.textField.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
self.textField.minimumFontSize = 10;
pragma mark - 编辑行为
//再次编辑就清空 默认为NOself.textField.clearsOnBeginEditing = YES;
//是否插入文本取代先前内容, 默认为NO
self.textField.clearsOnInsertion = NO;
//是否允许编辑文本属性, 默认为NO
self.textField.allowsEditingTextAttributes = NO;
pragma mark - 设置视图背景
//设置边框样式self.textField.borderStyle = UITextBorderStyleRoundedRect; /* typedef enum { UITextBorderStyleNone, 无边框样式, 默认为无 UITextBorderStyleLine, 边框样式为矩形 UITextBorderStyleBezel, 边框样式为表圈, 此样式通常被用在标准数据 输入 UITextBorderStyleRoundedRect 边框样式为四边有弧度的圆矩形 } UITextBorderStyle; */
//设置背景图片,如果使用了自定义的背景图片边框会被忽略掉
self.textField.background = [UIImage imageNamed:@""];
//设置背景图片, 如果background属性没有被设置, 此属性会被忽略
self.textField.disabledBackground = [UIImage imageNamed:@""];
pragma mark - 覆盖视图
//清除文本模式,用于一次性删除输入框中的内容self.textField.clearButtonMode = UITextFieldViewModeAlways; /* typedef enum { UITextFieldViewModeNever, 从不出现 UITextFieldViewModeWhileEditing, 编辑时出现 UITextFieldViewModeUnlessEditing, 除了编辑外都出现 UITextFieldViewModeAlways 一直出现 } UITextFieldViewMode; */
//左视图模式, 用于在何时出现该视图, 与清除文本模式相同
self.textField.leftViewMode = UITextFieldViewModeWhileEditing;
//设置左视图, 不要忘了打开视图模式
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 30)]; leftView.backgroundColor = [UIColor redColor]; self.textField.leftView = leftView; [leftView release];
//右视图模式, 用于在何时出现该视图, 与清除文本模式相同
self.textField.rightViewMode = UITextFieldViewModeWhileEditing; UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(140, 0, 10, 30)]; rightView.backgroundColor = [UIColor redColor]; self.textField.rightView = rightView; [rightView release];
pragma mark - 绘画界面
/* 下列方法, 不可以直接调用, 需要的话可以重写此方法, 返回值都为CGRect *///– textRectForBounds: 重写来重置文字区域 //- drawTextInRect: 改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. //- placeholderRectForBounds: 重写来重置占位符区域 //– drawPlaceholderInRect: 重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. //– borderRectForBounds: 重写来重置边缘区域 //– editingRectForBounds: 重写来重置编辑区域 //– clearButtonRectForBounds: 重写来重置clearButton位置,改变size可能导致button的图片失真 //– leftViewRectForBounds: 重写来重置左视图区域 //– rightViewRectForBounds: 重写来重置右视图区域
pragma mark - 代替输入
// inputView: 代替标准的系统键盘// inputAccessoryView: 编辑时显示在系统键盘或用户自定义的inputView上面的视图
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 20)]; inputView.backgroundColor = [UIColor redColor]; self.textField.inputAccessoryView = inputView;
//输入安全属性, 默认为NO
//textField.secureTextEntry = YES;
//自动更正功能
self.textField.autocorrectionType = UITextAutocorrectionTypeDefault; /* typedef NS_ENUM(NSInteger, UITextAutocorrectionType) { UITextAutocorrectionTypeDefault, UITextAutocorrectionTypeNo, UITextAutocorrectionTypeYes, }; */
pragma mark - 键盘相关 (键盘commond + K 弹出与收回)
//设置键盘的样式self.textField.keyboardType = UIKeyboardTypeDefault; /* typedef enum { UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, 数字键盘 UIKeyboardTypePhonePad, 电话键盘 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名 UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType; */
//首字母是否大写
self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone; /* typedef enum { UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写 } UITextAutocapitalizationType; */
//设置return键
self.textField.returnKeyType =UIReturnKeyDefault; /* typedef enum { UIReturnKeyDefault, 默认 灰色按钮,标有Return UIReturnKeyGo, 标有Go的蓝色按钮 UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索 UIReturnKeyJoin,标有Join的蓝色按钮 UIReturnKeyNext,标有Next的蓝色按钮 UIReturnKeyRoute,标有Route的蓝色按钮 UIReturnKeySearch,标有Search的蓝色按钮 UIReturnKeySend,标有Send的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyEmergencyCall, 紧急呼叫按钮 } UIReturnKeyType; */
//设置键盘外观
self.textField.keyboardAppearance=UIKeyboardAppearanceDefault; /* typedef enum { UIKeyboardAppearanceDefault, 默认外观,浅灰色 UIKeyboardAppearanceAlert, 深灰 石墨色 } UIReturnKeyType */
pragma mark - 访问代理
//设置代理 用于实现协议, 需要先签写self.textField.delegate = self;
/* 代理详情请看 “#pragma mark * 访问代理” /
/* 创建另一个对象 */
self.textField2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 30)]; self.textField2.placeholder = @"OK"; self.textField2.borderStyle = UITextBorderStyleRoundedRect; self.textField2.textAlignment = 1; self.textField2.delegate = self; [self.window addSubview:self.textField2]; return YES;
}
pragma mark ** 访问代理
//管理编辑/* 文本框应该开始编辑 BOOL值 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// return NO; /* 如果返回值为NO, 相当于禁止编辑 */
return YES;
}
/* 文本框已经开始编辑 */
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
/* 文本框应该结束编辑 BOOL值 */
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
/* 文本框已经结束编辑 */
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
//编辑文本
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
//这对于想要加入撤销选项的应用程序特别有用
//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
//要防止文字被改变可以返回NO
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
return YES;
}
/* 是否允许根据用户请求清除内容, 可以设置在特定条件下才允许清除内容 */
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.textField == textField) {
[self.textField2 becomeFirstResponder]; /* return键或回车键, 自动跳到下一个输入文本框 */
} else if (self.textField2 == textField)
{
[self.textField2 resignFirstResponder]; /* 按return键或回车键, 键盘往下收 becomeFirstResponder */
}
return YES;
}
/* 点击空白区域回收键盘 */
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
[self.window endEditing:YES];
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- ios通过按钮点击异步加载图片
- 加载带有手势识别器的XIB文件需注意哪些问题
- 仅需几行代码实现方便易用的状态栏指示器
- ios中图像进行压缩方法汇总