1.UITextField的初始化和設定 textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)]; [textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框類型 //如果出現UITextField不顯示的情況,很可能是沒設定其BorderStyle textField.placeholder = @"password"; //預設顯示的字 textField.secureTextEntry = YES; //密碼 textField.autocorrectionType = UITextAutocorrectionTypeNo; textField.autocapitalizationType = UITextAutocapitalizationTypeNone; textField.returnKeyType = UIReturnKeyDone; textField.clearButtonMode = UITextFieldViewModeWhileEditing; //編輯時會出現個修改X textField.delegate = self;2.要實現的Delegate方法,關閉鍵盤 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.textField resignFirstResponder]; return YES; } 3. 可以在UITextField使用下面方法,按return鍵返回-(IBAction) textFieldDone:(id) sender{ [textFieldName resignFirstResponder]; }連結TextField控制項的"Did end on exit"最右側加圖片是以下代碼, 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;查看函數的方法:按住command鍵雙擊進入函式宣告按住alt鍵雙擊進入doc文檔///////////////////////////////////////////////////////////////文字框常用方法:如何用程式刪除文字框中選中的文本[textView delete: nil];///////////////////////////////////////////////////////////////如何限制文字框只能輸入數字:建立NSNumberFormatter的子類,增加這個方法,將formatter連結至文字框。 - (BOOL) isPartialStringValid: (NSString **) partialStringPtr proposedSelectedRange: (NSRangePointer) proposedSelRangePtr originalString: (NSString *) origString originalSelectedRange: (NSRange) origSelRange errorDescription: (NSString **) error{ NSCharacterSet *nonDigits; NSRange newStuff; NSString *newStuffString; nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; newStuff = NSMakeRange(origSelRange.location, proposedSelRangePtr->location - origSelRange.location); newStuffString = [*partialStringPtr substringWithRange: newStuff]; if ([newStuffString rangeOfCharacterFromSet: nonDigits options: NSLiteralSearch].location != NSNotFound) { *error = @"不是數字"; return (NO); } else { *error = nil; return (YES); } }///////////////////////////////////////////////////////////////從文字框擷取十六進位資料的代碼char singleNumberString[3] = {'\0','\0','\0'};uint32_t singleNumber = 0;uint32_t i = 0; NSMutableData *data = [NSMutableData data]; //從文字框擷取到得資料 const char *buf = [[_hexToSendTextField text] UTF8String]; //轉換為十六進位 for(i = 0; i < strlen(buf); i+=2) { if(((i+1) < len && isxdigit(buf) && (isxdigit(buf[i+1]))) { singleNumberString[0] = buf; singleNumberString[1] = buf[i+1]; sscanf(singleNumberString, "%x", &singleNumber); [data appendBytes:(void*)(&tmp) length:1]; } else { break; } } //輸出 NSLog(@"%@", data);/////////////////////////////////////////////////////////////點擊 UITextView 輸入文字,游標都從最初點開始- (void)textViewDidChangeSelection:(UITextView *)textView{ NSRange range; range.location = 0; range.length = 0; textView.selectedRange = range;}///////////////////////////////////////////////////////////軟鍵盤在登入頁面要實現使用者名稱和密碼,密碼要是點點格式,引入當前頁面游標要停留在使用者名稱選項,軟鍵盤要彈出介面。如下圖:彈出鍵盤:[username becomeFirstResponder];取消鍵盤:[username resignFirstResponder];密碼保護:password.secureTextEntry=YES;//////////////////////////////////////////////////////////////////1.UITextField的初始化和設定 textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)]; [textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框類型 textField.placeholder = @"password"; //預設顯示的字 textField.secureTextEntry = YES; //密碼 textField.autocorrectionType = UITextAutocorrectionTypeNo; textField.autocapitalizationType = UITextAutocapitalizationTypeNone; textField.returnKeyType = UIReturnKeyDone; textField.clearButtonMode = UITextFieldViewModeWhileEditing; //編輯時會出現個修改X textField.delegate = self;2.要實現的Delegate方法,關閉鍵盤 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.textField resignFirstResponder]; return YES; } 3. 可以在UITextField使用下面方法,按return鍵返回-(IBAction) textFieldDone:(id) sender{ [textFieldName resignFirstResponder]; }連結TextField控制項的"Did end on exit"////////////////////////////////////////////////////////////////////限制輸入文本的長度- (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來隱藏鍵盤 在iPhone介面上,時常會需要當使用者輸入完內容後,隱藏鍵盤。 當然有很多方法,今天只介紹使用UITextFieldDelegate這個協議實現隱藏鍵盤。其實很簡單, 需要三步:1. 在你的控制器類中,加入UITextFieldDelegate這個協議如:@interface AddItemViewController : UIViewController <UITextFieldDelegate>2. 在使用了UITextFieldDelegate協議的控制器類的實現中,加入- (BOOL)textFieldShouldReturn:方法。- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES;} //設定焦點:[UITextField becomeFirstResponder];3. 將xib檔案中的TextField控制項的delegate變數指向到之前使用UITextFieldDelegate協議的那個控制器類,將 TextField的delegate IBOutlet變數右鍵連結到前面的控制器類的執行個體上。或者使用代碼方式,指定相關TextField的delegate變數。- (void)viewDidLoad { [super viewDidLoad]; itemNameField.delegate = self; priceField.delegate = self;}---------------------------
1.建立並初始化
建立UITextView的檔案,並在.h檔案中寫入如下代碼:
#import <UIKit/UIKit.h> @interface TextViewController : UIViewController <UITextViewDelegate>{ UITextView *textView;} @property (nonatomic, retain) UITextView *textView; @end
在.m檔案中初始化這個textview,寫入代碼如下:
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease]; //初始化大小並自動釋放 |
self.textView.textColor = [UIColor blackColor];//設定textview裡面的字型顏色 |
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//設定字型名字和字型大小 |
self.textView.delegate = self;//設定它的委託方法 |
self.textView.backgroundColor = [UIColor whiteColor];//設定它的背景顏色 |
self.textView.text = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";//設定它顯示的內容 |
self.textView.returnKeyType = UIReturnKeyDefault;//返回鍵的類型 |
self.textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型 |
self.textView.scrollEnabled = YES;//是否可以拖動 |
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應高度 |
[self.view addSubview: self.textView];//加入到整個頁面中 |
2. UITextView退出鍵盤的幾種方式
因為你點擊UITextView會出現鍵盤,如果你退出鍵盤,有如下幾種方式:
(1)如果你程式是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實UITextViewDelegate。
代碼如下:
- (void)textViewDidBeginEditing:(UITextView *)textView { |
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease]; |
self.navigationItem.rightBarButtonItem = done; |
- (void)textViewDidEndEditing:(UITextView *)textView { |
self.navigationItem.rightBarButtonItem = nil; |
[self.textView resignFirstResponder]; |
(2)如果你的textview裡不用斷行符號鍵,可以把斷行符號鍵當做退出鍵盤的響應鍵。
代碼如下:
#pragma mark - UITextView Delegate Methods |
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text |
if ([text isEqualToString:@"\n"]) { |
|
[textView resignFirstResponder]; |
|
這樣無論你是使用電腦鍵盤上的斷行符號鍵還是使用彈出鍵盤裡的return鍵都可以達到退出鍵盤的效果。
(3)還有你也可以自訂其他載入鍵盤上面用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。
代碼如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; |
[topView setBarStyle:UIBarStyleBlack]; |
|
UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil]; |
|
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; |
|
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)]; |
|
NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil]; |
|
[topView setItems:buttonsArray]; |
|