#import "ViewController.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#define kDebugPrint NSLog(@"%s %d",__func__,__LINE__)
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatTextField];
}
//- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//
// return YES;
//
//}
#pragma mark - 文本输入框
- (void)creatTextField {
NSInteger space = 10;
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(space, 30, kScreenSize.width-2*space, 30)];
//设置背景
//textField1.backgroundColor = [UIColor redColor];
//设置边框类型
/*
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
*/
textField1.borderStyle = UITextBorderStyleLine;
//设置提示语
textField1.placeholder = @"请输入内容";
//设置对齐方式 水平
textField1.textAlignment = NSTextAlignmentCenter;
//竖直对齐
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//顶部对齐
//设置字体大小
textField1.font = [UIFont systemFontOfSize:25];
//字体大小 宽度自适应
textField1.adjustsFontSizeToFitWidth = YES;
//设置自适应滚动效果的字体最小值
textField1.minimumFontSize = 20;
//设置字体颜色
textField1.textColor = [UIColor redColor];
//再次进入编辑模式 是否清除之前内容
textField1.clearsOnBeginEditing = YES;
//设置 右侧清除按钮 小叉子
/*
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,编辑的时候显示
UITextFieldViewModeUnlessEditing,输入内容之后退出编辑模式的时候(不编辑的时候)
UITextFieldViewModeAlways
*/
textField1.clearButtonMode = UITextFieldViewModeUnlessEditing;
//设置键盘的风格
textField1.keyboardAppearance = UIKeyboardAppearanceDark;
//设置键盘的类型 电话键盘 数字键盘 邮箱键盘
//textField1.keyboardType = UIKeyboardTypeNumberPad;
//设置return键
/*
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
*/
textField1.returnKeyType = UIReturnKeySend;
//设置输入内容的首字母是否大写
/*
UITextAutocapitalizationTypeNone, 都不大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences,句子首字母大写
UITextAutocapitalizationTypeAllCharacters,都大写
*/
textField1.autocapitalizationType = UITextAutocapitalizationTypeWords;
/*
UITextAutocorrectionTypeDefault 自动纠错
UITextAutocorrectionTypeNo, 不纠错
UITextAutocorrectionTypeYes, 自动纠错
*/
//设置自动纠错
textField1.autocorrectionType = UITextAutocorrectionTypeYes;
[self.view addSubview:textField1];
[textField1 release];
//根据textField1.frame 获取 frame y+height的值
CGFloat y = CGRectGetMaxY(textField1.frame);
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(space, y+space, kScreenSize.width-2*space, 30)];
textField2.borderStyle = UITextBorderStyleLine;
//密文显示
textField2.secureTextEntry = YES;
//获取内容
//textField2.text
[self.view addSubview:textField2];
[textField2 release];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"range:%@",NSStringFromRange(range));
NSLog(@"string:%@",string);
//限制密码textField 输入只能输入6位
if (textField.tag == 102) {//密码textField
//将要输入的字符长度 + 已经输入的字符长度 <= 6
return textField.text.length+string.length <= 6;
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
原文:http://my.oschina.net/u/2410306/blog/523199