IOS UITextField

// UITextField的初始化
api

 UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320,30)];
函數


//外框類型 spa

[textField setBorderStyle:UITextBorderStyleRoundedRect]; 繼承


 //默認顯示的字 事件

 textField.placeholder = @"默認顯示的字";圖片


 //密碼string

 textField.secureTextEntry = YES; it


//是否糾錯io

  text.autocorrectionType = UITextAutocorrectionTypeNo;file

typedef enum {

    UITextAutocorrectionTypeDefault, 默認

    UITextAutocorrectionTypeNo,   不自動糾錯

    UITextAutocorrectionTypeYes,  自動糾錯

} UITextAutocorrectionType;


//首字母是否大寫

 textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 

typedef enum {

    UITextAutocapitalizationTypeNone, 不自動大寫

    UITextAutocapitalizationTypeWords,  單詞首字母大寫

    UITextAutocapitalizationTypeSentences,  句子的首字母大寫

    UITextAutocapitalizationTypeAllCharacters, 全部字母都大寫

} UITextAutocapitalizationType;


 textField.returnKeyType = UIReturnKeyDone;
 textField.clearButtonMode = UITextFieldViewModeWhileEditing; //編輯時會出現個修改X

//按return鍵返回
-(IBAction) textFieldDone:(id) sender
{
[textFieldName resignFirstResponder];
}
最右側加圖片是如下代碼,
   UIImageView *imgv=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
   text.rightView=imgv;
   text.rightViewMode = UITextFieldViewModeAlways;    

若是是在最左側加圖片就換成:
text.leftView=imgv;
text.leftViewMode = UITextFieldViewModeAlways;    
UITextField 繼承自 UIControl,此類中有一個屬性contentVerticalAlignment
因此想讓UITextField裏面的text垂直居中能夠這樣寫:
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

//刪除文本框中選中的文本
[textView delete: nil]; 

//限制長度

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if (range.location >= MAX_LENGTH)
       return NO; // return NO to not change text
   return YES;
}
if (textField.text.length >= 10 && range.length == 0)
   return NO;
return YES;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([textField.text length] > MAXLENGTH)
{
 textField.text = [textField.text substringToIndex:MAXLENGTH-1];
 return NO;
}
return YES;

}


// UITextFieldDelegate 協議

<UITextFieldDelegate>


//按鍵盤完成時取消焦點:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

       [textField resignFirstResponder]; //取消焦點/隱藏鍵盤

       return YES;

}


//設置焦點:
[UITextField becomeFirstResponder];

//綁定事件
textField.delegate = self;

ps:

有時可能會遇到 textFieldShouldReturn 函數寫了,可是按鍵到 return 沒法讓鍵盤消失。這是由於你的文本框沒有添加委託。添加委託的方法,右鍵文本框,把 outlets 下的+拉到 file's owner 上就能夠了。 或者在加載事件中添加t xtLength.delegate=self;

相關文章
相關標籤/搜索