UI 控件 —UITextFile
生活随笔
收集整理的這篇文章主要介紹了
UI 控件 —UITextFile
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自:http://blog.csdn.net/weisubao/article/details/39609579
?
(1)可以根據需要設置文本框的樣式(包括形狀、邊框顏色、背景等)。 (2)可以根據需要設置文字顯示樣式(包括輸入密碼時的密文顯示、文字橫向居中、縱向居中上下、輸入的文字是否首席木大寫、文字超過后是否縮小還是向右滾動等)。 (3)可以根據需要設置各種不同的鍵盤樣式(只有數字、只有字母等等)。 (4)還有inputView可以彈出一個視圖,用于取代彈出鍵盤,暫時不知道什么用處,但貌似可以用得地方很多啊。 (5)還有return的樣式設置,可以設置為Google也可以設置為Go和Search等更形象的按鈕。 (6)還有一個clearsOnBeginEditing是否設置清除按鈕也很常用。 (7)還有用得比較多得估計是左右視圖,也就是我們常見的用戶名和密碼的前面還有一個小icon圖片表示用戶的“小人”和表示密碼的“鎖”的圖片,用左右視圖可以加載進來,當然最后要記得設置左右視圖模式為Always,不然默認是Never不顯示的。?
[objc]?view plaincopy3.設置屬性
?
擴展屬性
UIControl屬性對UITextField完全可以用,下面的都是UITextField擴展的屬性
01.myTextField.textAlignment = UITextAlignmentLeft;//默認就是左對齊,這個是UITextField擴展屬性?? 02.myTextField.borderStyle = UITextBorderStyleBezel;//默認是沒有邊框,如果使用了自定義的背景圖片邊框會被忽略掉?? 03.myTextField.placeholder = @"請在此輸入賬號";//為空白文本字段繪制一個灰色字符串作為占位符?? 04.myTextField.clearsOnBeginEditing = YES;//設置為YES當用點觸文本字段時,字段內容會被清除?? 05.myTextField.adjustsFontSizeToFitWidth = YES;//設置為YES時文本會自動縮小以適應文本窗口大小。默認是保持原來大小,而讓長文本滾動?? 06.//myTextField.background = [UIImage imageNamed:@"registBtn"];//可以接受UIImage對象,此項設置則邊框失效。?? 07.myTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;//右邊顯示的'X'清楚按鈕?? 08.//myTextField.LeftView =?? 09.//myTextField.leftViewMode =??? 10.//myTextField.RightView =?? 11.//myTextField.rightViewMode =???? 4.下列方法在創建一個UITextField的子類時可以重寫:borderRectForBounds指定矩形邊界textRectForBounds 指定顯示文本的邊界placeholderRectForBounds指定站位文本的邊界editingRectForBounds指定編輯中文本的邊界clearButtonRectForBounds指定顯示清除按鈕的邊界leftViewRectForBounds指定顯示左附著視圖的邊界rightViewRectForBounds指定顯示右附著視圖的邊界委托方法。 ======================================== 01.- (CGRect)clearButtonForBounds:(CGRect)bounds{?? 02.????return CGRectMake(bounds.origin.x +bounds.size.width-50,??? 03.??????????????????????bounds.origin.y+bounds.size.height-20, 16, 16);?? 04.}?? ======================================== 01.- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{?? 02.????//返回一個BOOL值,指定是否循序文本字段開始編輯?? 03.????return YES;?? 04.}?? ======================================== 01.- (void)textFieldDidBeginEditing:(UITextField *)textField{?? 02.????//開始編輯時觸發,文本字段將成為first responder?? 03.}?? ======================================== 01.- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{?? 02.????//返回BOOL值,指定是否允許文本字段結束編輯,當編輯結束,文本字段會讓出first responder?? 03.????//要想在用戶結束編輯時阻止文本字段消失,可以返回NO?? 04.????//這對一些文本字段必須始終保持活躍狀態的程序很有用,比如即時消息?? 05.????return NO;?? 06.}?? ======================================== 01.- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{?? 02.????//當用戶使用自動更正功能,把輸入的文字修改為推薦的文字時,就會調用這個方法。?? 03.????//這對于想要加入撤銷選項的應用程序特別有用?? 04.????//可以跟蹤字段內所做的最后一次修改,也可以對所有編輯做日志記錄,用作審計用途。????? 05.????//要防止文字被改變可以返回NO?? 06.????//這個方法的參數中有一個NSRange對象,指明了被改變文字的位置,建議修改的文本也在其中?? 07.????return YES;?? 08.}?? ======================================== 01.- (BOOL)textFieldShouldClear:(UITextField *)textField{?? 02.????//返回一個BOOL值指明是否允許根據用戶請求清除內容?? 03.????//可以設置在特定條件下才允許清除內容?? 04.????return YES;?? 05.}?? ======================================== 01.-(BOOL)textFieldShouldReturn:(UITextField *)textField{?? 02.????//返回一個BOOL值,指明是否允許在按下回車鍵時結束編輯?? 03.????//如果允許要調用resignFirstResponder 方法,這回導致結束編輯,而鍵盤會被收起?? 04.????[textField resignFirstResponder];//查一下resign這個單詞的意思就明白這個方法了?? 05.????return YES;?? 06.}?? ======================================== 虛擬鍵盤擋住UITextField時的解決方法:RootViewController.h 中: #import <UIKit/UIKit.h> @interface RootViewController : UIViewController<UITextFieldDelegate> { UITextField *textField1; UITextField *textField2; } @property (nonatomic,retain) UITextField *textField1; @property (nonatomic ,retain) UITextField *textField2; -(IBAction)backgroundTap:(id)sender; @end RootViewController.m 中: #import "RootViewController.h" @implementation RootViewController @synthesize textField1; @synthesize textField2; // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. /* - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization. } return self; } */ /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { UIView *back = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]]; back.backgroundColor = [UIColor grayColor]; self.view = back; [back release]; } */ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame]; _back.backgroundColor = [UIColor grayColor]; self.view = _back; [_back release]; [(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown]; textField1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 200, 30)]; textField1.backgroundColor = [UIColor clearColor]; textField1.borderStyle = UITextBorderStyleRoundedRect; textField1.textColor = [UIColor redColor]; textField1.delegate = self; [self.view addSubview:textField1]; textField2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 200, 30)]; textField2.backgroundColor = [UIColor clearColor]; textField2.borderStyle = UITextBorderStyleRoundedRect; textField2.textColor = [UIColor redColor]; textField2.delegate = self; [self.view addSubview:textField2]; } #pragma mark - #pragma mark 解決虛擬鍵盤擋住UITextField的方法 - (void)keyboardWillShow:(NSNotification *)noti {? //鍵盤輸入的界面調整? //鍵盤的高度 float height = 216.0;? CGRect frame = self.view.frame;? frame.size = CGSizeMake(frame.size.width, frame.size.height - height);? [UIView beginAnimations:@"Curl"context:nil];//動畫開始? [UIView setAnimationDuration:0.30];? [UIView setAnimationDelegate:self];? [self.view setFrame:frame];? [UIView commitAnimations]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField {? // When the user presses return, take focus away from the text field so that the keyboard is dismissed.? NSTimeInterval animationDuration = 0.30f;? [UIView beginAnimations:@"ResizeForKeyboard" context:nil];? [UIView setAnimationDuration:animationDuration];? CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);? self.view.frame = rect; [UIView commitAnimations]; [textField resignFirstResponder]; return YES;? } - (void)textFieldDidBeginEditing:(UITextField *)textField {? CGRect frame = textField.frame; int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//鍵盤高度216 NSTimeInterval animationDuration = 0.30f;? [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];? [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width;? float height = self.view.frame.size.height;? if(offset > 0) { CGRect rect = CGRectMake(0.0f, -offset,width,height);? self.view.frame = rect;? }? [UIView commitAnimations];? } #pragma mark - #pragma mark 觸摸背景來關閉虛擬鍵盤 -(IBAction)backgroundTap:(id)sender { // When the user presses return, take focus away from the text field so that the keyboard is dismissed.? NSTimeInterval animationDuration = 0.30f;? [UIView beginAnimations:@"ResizeForKeyboard" context:nil];? [UIView setAnimationDuration:animationDuration];? CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);? self.view.frame = rect; [UIView commitAnimations]; [textField1 resignFirstResponder]; [textField2 resignFirstResponder]; } #pragma mark - /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations. return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc. that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [textField1 release]; [textField2 release]; [super dealloc]; } RootViewController.m 中的backgroundTap:方法,用來實現觸摸背景來關閉虛擬鍵盤。 這個方法用的時候首先把RootViewController上的view改成UIControl,然后通過UIControl的事件UIControlEventTouchDown來觸發上面的方法backgroundTap: 。 注意下面的代碼: UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame]; _back.backgroundColor = [UIColor grayColor]; self.view = _back; [_back release]; [(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown]; 解決textField被鍵盤擋住的問題的方法有三個: - (void)keyboardWillShow:(NSNotification *)noti;//調整虛擬鍵盤與self.view之間的關系。 -(BOOL)textFieldShouldReturn:(UITextField *)textField;//觸摸鍵盤上的return鍵時關閉虛擬鍵盤 - (void)textFieldDidBeginEditing:(UITextField *)textField;//當編輯文本的時候,如果虛擬鍵盤擋住了textField,整個view就會向上移動。移動范圍是一個鍵盤的高度216。
轉載于:https://www.cnblogs.com/Always-LuoHan/p/5267317.html
總結
以上是生活随笔為你收集整理的UI 控件 —UITextFile的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS判断是模拟器还是真机
- 下一篇: MySql连接——内连接、外连接(左连接