titleTextField.becomeFirstResponder() 设置为第一响应者
/初始化textfield并设置位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
text.backgroundColor = [UIColor whiteColor];
text.background = [UIImage imageNamed:@"dd.png"];
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
text.placeholder = @"password";
text.font = [UIFont fontWithName:@"Arial" size:20.0f];
text.textColor = [UIColor redColor];
text.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways 一直出现
} UITextFieldViewMode;
text.text = @"一开始就在输入框的文字";
text.secureTextEntry = YES;
text.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;
text.clearsOnBeginEditing = YES;
text.textAlignment = UITextAlignmentLeft;
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textFied.adjustsFontSizeToFitWidth = YES;
text.minimumFontSize = 20;
text.keyboardType = UIKeyboardTypeNumberPad;
typedef enum {
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
text.returnKeyType =UIReturnKeyDone;
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert, 深灰 石墨色
} UIReturnKeyType;
text.delegate = self;
[self.window addSubview:text];
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
类要采用UITextFieldDelegate协议
text.delegate = self; 声明text的代理是我,我会去实现把键盘往下收的方法 这个方法在UITextFieldDelegate里所以我们要采用UITextFieldDelegate这个协议
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[text resignFirstResponder];
return YES;
}
重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。
– textRectForBounds:
– drawTextInRect:
– placeholderRectForBounds:
– drawPlaceholderInRect:
– borderRectForBounds:
– editingRectForBounds:
– clearButtonRectForBounds:
– leftViewRectForBounds:
– rightViewRectForBounds:
委托方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return NO;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
UITextField 限制输入字数
UITextField 限制输入字数
方法一
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (range.location>= 11)
return NO;
returnYES;
}
方法二
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (toBeString.length > 11) {
textField.text = [toBeString substringToIndex:11];
return NO;
}
returnYES;
}
当前光标位置 range.location
已选文字长度 range.length
输入文字长度 textView.text.length
已有文字长度 text.length
————mark 消息机制侦听更改 ----
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldChanged:)
name:UITextFieldTextDidChangeNotification
object:textField];
searchText = UITextField(frame: CGRect(x: interval_W, y: logoImageView.center.y+logoImageView.bounds.height*1.5, width: stageWidth*(500/640), height: stageWidth*(70/640)))
searchText.layer.borderColor = violetColor.CGColor
searchText.layer.borderWidth = 1.0
searchText.layer.masksToBounds = true
searchText.placeholder = " 请输入舞曲或者舞队名称"
searchText.addTarget(self, action: "searchTextChange:", forControlEvents: UIControlEvents.EditingChanged)
searchimage = UIImageView(image: UIImage(named: "ser_serch.png"))
searchimage.center.x = searchimage.bounds.width
searchimage.center.y = searchText.bounds.height/2
searchText.addSubview(searchimage)
searchText.delegate = self
searchText.clearButtonMode = UITextFieldViewMode.Always
searchText.returnKeyType = UIReturnKeyType.Search
self.view.addSubview(searchText)
-----
func searchTextChange(text:UITextField){
if text.text == nil || text.text == ""{
searchimage.hidden = false
}else{
searchimage.hidden = true
}
}
全局的取消键盘响应者
- (IBAction)View_TouchDown:(id)sender {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
UIApplication.sharedApplication().sendAction("resignFirstResponder", to: nil, from: nil, forEvent: nil)