日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UI   控件 —UITextFile

發布時間:2024/6/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 plaincopy
  • -?(void)viewDidLoad?{??
  • ????//textfiled1本想是textField1的,但不影響??
  • ????UITextField?*textFiled1=[[UITextField?alloc]init];??
  • ????//此時textField1已存在,但因為是透明背景,所以看不見,但是點擊那塊地方會發現光標閃爍可寫??
  • ????//為了證明是透明背景而不是白色背景,我們可以設置self.view背景為紅色,看看textField1是白色還是透明色??
  • //????self.view.backgroundColor=[UIColor?redColor];??
  • ????textFiled1.frame=CGRectMake(10,?30,?300,?30);??
  • ????//設置邊框樣式??
  • ????//UITextBorderStyleRoundedRect-圓角矩形,背景是白色,不再是透明的??
  • ????//UITextBorderStyleLine-矩形,黑色邊框,透明背景??
  • ????//UITextBorderStyleBezel-和上面類似,但是是灰色的邊框,背景透明??
  • ????textFiled1.borderStyle=UITextBorderStyleRoundedRect;??
  • ????//設置背景顏色,會覆蓋上面圓角矩形默認的白色背景??
  • ????textFiled1.backgroundColor=[UIColor?purpleColor];??
  • ????//設置提示(默認)文字??
  • ????textFiled1.placeholder=@"請輸入您的密碼";??
  • ????//設置密文輸入,就是和輸入密碼時類似的顯示為小圓點??
  • ????textFiled1.secureTextEntry=YES;??
  • ????//設置鍵盤樣式,比如銀行取款密碼只需要數字,有的輸入郵箱需要@等等??
  • ????//UIKeyboardTypeAlphabet和UIKeyboardTypeDefault類似,就是我們平時看到那樣,都是字母,然后有個按鍵可以切換符號??
  • ????//UIKeyboardTypeASCIICapable好像和上面差不多??
  • ????//UIKeyboardTypeDecimalPad,UIKeyboardTypeNumberPad都是數字,但前者多了一個“小數點”按鍵??
  • ????//UIKeyboardTypeEmailAddress-除了字母還有小數點和@出現??
  • ????//UIKeyboardTypeNamePhonePad-貌似正常??
  • ????//UIKeyboardTypePhonePad-電話鍵盤,不僅有數字還有*和#的那種??
  • ????//UIKeyboardTypeNumbersAndPunctuation-只有數字和標點符號??
  • ????//UIKeyboardTypeTwitter-除了字母還有@和#,這是微博的符號??
  • ????//UIKeyboardTypeURL-除字母,還有.com按鈕,方便輸入??
  • ????//UIKeyboardTypeWebSearch-主要區別在于return鍵變成了GO鍵??
  • ????//注意:如果是最xcode6下的模擬器的話,默認是不調出軟鍵盤的,按CMD+K可以調出,或者在菜單Hardware里地Keyboard里設置??
  • ????textFiled1.keyboardType=UIKeyboardTypeWebSearch;??
  • ????//設置鍵盤外觀??
  • ????//UIKeyboardAppearanceDark和UIKeyboardAppearanceAlert都是把鍵盤背景變成半透明灰色區別不明顯??
  • ????//UIKeyboardAppearanceLight貌似和UIKeyboardAppearanceDefault一樣,沒啥區別??
  • ????textFiled1.keyboardAppearance=UIKeyboardAppearanceAlert;??
  • ??????
  • ????//設置彈出視圖,inputView即彈出的不是鍵盤而是這個視圖??
  • ????//設置的frame時,只有高度有用,其他x和y和寬都是無效的,寬是默認的整個鍵盤寬度??
  • ????UIImageView?*imgView1=[[UIImageView?alloc]initWithImage:[UIImage?imageNamed:@"logo-60@3x.png"]];??
  • ????imgView1.frame=CGRectMake(60,?60,?300,?300);??
  • ????textFiled1.inputView=imgView1;??
  • ??????
  • ????//設置左視圖,就是用戶名和密碼,有時候放個圖片的位置??
  • ????UIView?*view1=[[UIView?alloc]init];??
  • ????//x和y無效,x都是0,而y是根據高度來自動調整的。即高度如果超過textField則默認是textField高,如小于textField高度,則上下居中顯示。唯一有效的就是寬度??
  • ????view1.frame=CGRectMake(10,?500,?50,?10);??
  • ????view1.backgroundColor=[UIColor?orangeColor];??
  • ????textFiled1.leftView=view1;??
  • ????//最重要的時:默認它是不顯示的即UITextFieldViewModeNever,我們可以設置永遠顯示UITextFieldViewModeAlways??
  • ????//UITextFieldViewModeUnlessEditing-一開始就有,點擊框,呃,貌似還有??
  • ????//UITextFieldViewModeWhileEditing-一開始沒有,點擊框就出現??
  • ????textFiled1.leftViewMode=UITextFieldViewModeAlways;??
  • ??????
  • ????//同樣,我們可以設置右視圖,當然也可以加載和圖片進來??
  • ????UIImageView?*imgView2=[[UIImageView?alloc]initWithImage:[UIImage?imageNamed:@"logo-60@3x.png"]];??
  • ????imgView2.frame=CGRectMake(10,?500,?50,?10);??
  • ????textFiled1.rightView=imgView2;??
  • ????textFiled1.rightViewMode=UITextFieldViewModeAlways;??
  • ??????
  • ????//設置清除按鈕,就是那個叉叉X,一點擊整個輸入框的文字全部刪除重新輸入的那個X(我們先注釋掉不讓右視圖顯示,來查看效果)??
  • ????//其實我們在寫clearButtonMode是它又提示說這是一個UITextFieldViewMode類型,所以也是和上面一樣??
  • ????textFiled1.clearButtonMode=UITextFieldViewModeWhileEditing;??
  • ??????
  • ????//再次編輯時是否清空內容,這個除特定場景外很少用,會讓用戶抓狂的??
  • ????//當然為了模擬再次編輯,我們需要鼠標點到其他地方然后再點回來,所以再創建一個textField??
  • ????textFiled1.clearsOnBeginEditing=NO;??
  • ????//這個clearsOnInsertion貌似點擊回去再次編輯時不清楚,但是只要一輸入內容就會清除之前的??
  • ????textFiled1.clearsOnInsertion=YES;??
  • ????UITextField?*textField2=[[UITextField?alloc]init];??
  • ????textField2.frame=CGRectMake(10,?80,?300,?100);??
  • ????textField2.borderStyle=UITextBorderStyleRoundedRect;??
  • ????[self.view?addSubview:textField2];??
  • ??????
  • ????//我們用上面創建的textField2來做如下??
  • ????//縱向對齊方式,默認是居中??
  • ????//UIControlContentVerticalAlignmentCenter居中,所以Top、Bottom就是居上居下。Fill貌似和Top差不多??
  • ????textField2.contentVerticalAlignment=UIControlContentVerticalAlignmentFill;??
  • ????//當然還有橫向對齊??
  • ????//也有左中右和Fill四種,但是貌似沒看到什么效果,可能對文字無效,因為有專門的針對文字的設置??
  • ????textField2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;??
  • ????//設置文字對齊方式??
  • ????//同樣我們輸入textAlignment時有提示是NSTextAlignment類型,有好幾種,不細講??
  • ????textField2.textAlignment=NSTextAlignmentCenter;??
  • ????//設置調整文字大小以適配寬度(即輸入不下時縮小文字,實在縮小不了了,就向后滾動),默認是向右滾動的??
  • ????textField2.adjustsFontSizeToFitWidth=YES;??
  • ????//設置最小字號,和上面有關,即小于這個字號的時候,我就不縮小了,直接向右滾動??
  • ????textField2.minimumFontSize=2;??
  • ????//設置字母大小樣式,輸入autocapitalizationType時有提示是UITextAutocapitalizationType類型??
  • ????//UITextAutocapitalizationTypeAllCharacters-所有字母大寫(用鍵盤輸入的話發現失效,需要用軟鍵盤輸入才有效,以下同理)??
  • ????//UITextAutocapitalizationTypeWords-單詞首字母大寫??
  • ????//UITextAutocapitalizationTypeSentences-句首字母大寫??
  • ????textField2.autocapitalizationType=UITextAutocapitalizationTypeSentences;??
  • ??????
  • ????//設置return樣式,有Done/Go/Next/Join/Google/Search/Yahoo/EmergencyCall/Send等,除了默認外,其他的按鈕都是藍顏色背景??
  • ????textField2.returnKeyType=UIReturnKeyEmergencyCall;??
  • ??????
  • ????[self.view?addSubview:textFiled1];??
  • ??????
  • ????[super?viewDidLoad];??
  • ????//?Do?any?additional?setup?after?loading?the?view,?typically?from?a?nib.??
  • } ?
  • [摘要:1.建立 01.UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)]; 2.設置托付 01.myTextField.delegate = self;//托付類須要恪守UITextFieldDelegate協定 3. 設置屬性 擴大屬性 UIControl屬性]? 1.創建 01.UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];?? 2.設置委托 01.myTextField.delegate = self;//委托類需要遵守UITextFieldDelegate協議??

    3.設置屬性

    ?

    擴展屬性

    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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。