UITextfield*textfield
= [[
UITextField
alloc
]
init
];
textfield . frame = CGRectMake ( 55 , 78 , 235 , 37 );
textfield . frame = CGRectMake ( 55 , 78 , 235 , 37 );
textfield没有输入前显示的字
textfield . placeholder = @"11234" ;
textfield . placeholder = NSLocalizedString ( @" 用户账号 " , nil );
textfield . placeholder = @"11234" ;
textfield . placeholder = NSLocalizedString ( @" 用户账号 " , nil );
背景颜色
textfield . backgroundColor = [ UIColor whiteColor ];
textfield . font = [ UIFont fontWithName : @"Helvetica-Bold" size : 15 ];
textfield . backgroundColor = [ UIColor whiteColor ];
textfield . font = [ UIFont fontWithName : @"Helvetica-Bold" size : 15 ];
代理
textfield . delegate = self ;
textfield . delegate = self ;
内容对齐
textfield . contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter ;
textfield . contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter ;
边框样式
textfield . borderStyle = UITextBorderStyleRoundedRect ;
textfield . borderStyle = UITextBorderStyleRoundedRect ;
键盘类型
textfield . keyboardType = UIKeyboardTypeNumberPad ;
textfield . keyboardType = UIKeyboardTypeNumberPad ;
自动适应大小
textfield . adjustsFontSizeToFitWidth = NO ;
textfield . adjustsFontSizeToFitWidth = NO ;
自动校正类型
textfield
.
autocorrectionType
=
UITextAutocorrectionTypeNo
;
自动大写输入
textfield . autocapitalizationType = UITextAutocapitalizationTypeNone ;
textfield . autocapitalizationType = UITextAutocapitalizationTypeNone ;
清除的按钮模式
textfield . clearButtonMode = UITextFieldViewModeAlways ;
textfield . textColor = [ UIColor whiteColor ];
[ self . view addSubview : textfield ];
textfield . clearButtonMode = UITextFieldViewModeAlways ;
textfield . textColor = [ UIColor whiteColor ];
[ self . view addSubview : textfield ];
#pragma mark - UITextFieldDelegate
- ( BOOL )textFieldShouldReturn:( UITextField *)textField // called when 'return' key pressed. return NO to ignore.
{
[ self . textfield resignFirstResponder ];
[ self . textfield resignFirstResponder ];
return YES ;
}
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event // 点击空白处键盘消失
{
[ self . textfield resignFirstResponder ];
[ self . textfield resignFirstResponder ];
}
- ( BOOL )canPerformAction:( SEL )action withSender:( id )sender // 限制不能粘贴复制
{
UIMenuController *menuController = [ UIMenuController sharedMenuController ];
if (menuController) {
[ UIMenuController sharedMenuController ]. menuVisible = NO ;
}
return NO ;
}
- ( BOOL )textField:( UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString *)string
{
NSString *newString;
if (range. length == 0 ) {
newString = [textField. text stringByAppendingString :string];
}
return newString. length <= 20 ? YES : NO ;
}
- ( BOOL )textFieldShouldReturn:( UITextField *)textField // called when 'return' key pressed. return NO to ignore.
{
[ self . textfield resignFirstResponder ];
[ self . textfield resignFirstResponder ];
return YES ;
}
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event // 点击空白处键盘消失
{
[ self . textfield resignFirstResponder ];
[ self . textfield resignFirstResponder ];
}
- ( BOOL )canPerformAction:( SEL )action withSender:( id )sender // 限制不能粘贴复制
{
UIMenuController *menuController = [ UIMenuController sharedMenuController ];
if (menuController) {
[ UIMenuController sharedMenuController ]. menuVisible = NO ;
}
return NO ;
}
- ( BOOL )textField:( UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString *)string
{
NSString *newString;
if (range. length == 0 ) {
newString = [textField. text stringByAppendingString :string];
}
return newString. length <= 20 ? YES : NO ;
}