UITextField經常使用屬性

UITextField是咱們常常用的之一可是經常使用的屬性都很熟悉,有些不經常使用的我也總結下,例以下面的: php

        UIImageView * myView = [[ UIImageView  alloc]initWithImage:[UIImage imageNamed:@"face.png"]]; html

UIImageView * myView2 = [[ UIImageView  alloc]initWithImage:[UIImage imageNamed:@"face.png"]]; java

UITextField *myTextField=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 240, 60)]; //初始化一個UITextField的frame objective-c

myTextField.textColor=[UIColor redColor];  //UITextField 的文字顏色 app

myTextField.delegate=self;//UITextField 代理方法設置 函數

myTextField.placeholder=@"輸入密碼";//UITextField 的初始隱藏文字,固然這個文字的字體大小顏色均可以改,重寫uitextfield,下次介紹 字體

myTextField.textAlignment=UITextAlignmentCenter;//UITextField 的文字對齊格式 ui

myTextField.font=[UIFont fontWithName:@"Times New Roman" size:30];//UITextField 的文字大小和字體 this

myTextField.adjustsFontSizeToFitWidth=YES;//UITextField 的文字自適應 spa

myTextField.clearsOnBeginEditing=NO;//UITextField 的是否出現一件清除按鈕

myTextField.borderStyle=UITextBorderStyleNone;//UITextField 的邊框

myTextField.background=[UIImage imageNamed:@"my.png"];//UITextField 的背景,注意只有UITextBorderStyleNone的時候改屬性有效

myTextField.clearButtonMode=UITextFieldViewModeNever;//UITextField 的一件清除按鈕是否出現

myTextField.leftView=myView;//UITextField 的左邊view

myTextField.leftViewMode=UITextFieldViewModeAlways;//UITextField 的左邊view 出現模式

myTextField.rightView=myView2;//UITextField 的有邊view

myTextField.rightViewMode=UITextFieldViewModeAlways;//UITextField 的右邊view 出現模式

myTextField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//UITextField 的字的擺設方式

[myView release];

[myView2 release];

[self.view addSubview:myTextField];


固然myTextField的鍵盤的出現也隱藏也能夠設置:

顯示keyboard:

[myTextField becomeFirstResponder];

隱藏keyboard

[myTextField resignFirstResponder];

myTextField.contentVerticalAlignment的值的種類:

typedef enum {
    UIControlContentVerticalAlignmentCenter  = 0,
    UIControlContentVerticalAlignmentTop     = 1,
    UIControlContentVerticalAlignmentBottom  = 2,
    UIControlContentVerticalAlignmentFill    = 3,
} UIControlContentVerticalAlignment;




  轉載別人的。。。

// 限定字符串

在 Xcode interface builder 能夠經過可視化 設置輸入框 的 屬性 在Attribute inspector 裏面把輸入設成Numberpad (在Xcode4.2中 爲 keyboard屬性 選擇Number Pad)

可是若是用戶在輸入框直接粘貼其餘地方複製的非數字的話,鍵盤限制就無用了

可使用 UITextField 的代理Delegate 方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
看以下例子
其中 

  1. #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
  2. #define NUMBERS @"0123456789"
  3. #define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
  4. #define NUMBERSPERIOD @"0123456789."
  5. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  6. {NSCharacterSet *cs;
  7. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD ] invertedSet]; //invertedSet 方法是去反字符,把全部的除了數字的字符都找出來
  8. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  //componentsSeparatedByCharactersInSet 方法是把輸入框輸入的字符string 根據cs中字符一個一個去除cs字符並分割成單字符並轉化爲 NSArray, 而後componentsJoinedByString 是把NSArray 的字符經過 ""無間隔鏈接成一個NSString字符 賦給filtered.就是隻剩數字了.
  9. BOOL basicTest = [string isEqualToString:filtered];
  10. if(!basicTest)
  11.         {
  12.             UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
  13.                                                             message:@"請輸入數字"
  14.                                                            delegate:nil
  15.                                                   cancelButtonTitle:@"肯定"
  16.                                                   otherButtonTitles:nil];
  17.             
  18.             [alert show];
  19.             [alert release];
  20.             return NO;
  21.         }    
  22. // Add any predicate testing here
  23. return basicTest;
  24. }
NSString 官方文檔
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html

NSArray 官方文檔
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/c_ref/NSArray


參考

http://guxiaojje.blog.163.com/blog/static/14094229120104344438786/
http://portal.devdiv.com/thread-70160-1-1.html


在UISearchBar中,當輸入信息改變時,它就會調用textDidChange函數,可是UITextField沒有這個功能,惟一與這個相似的shouldChangeCharactersInRange函數,也是在文件尚未改變前就調用了,而不是在改變後調用,要想實現這個功能,咱們能夠增長事件監聽的方式,這個與java的listener相似.先來看看objective-c提供的接口:
  1. // add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
  2. // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
  3. // the action cannot be NULL.
  4. - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

怎麼去使用這個接口呢?主要分爲兩步,第一步就是在UItextField組件中增長對文件編輯改變時事件的監聽,而後再實現監聽器監聽到事件時,所調用的方法.
  1. //第一步,對組件增長監聽器 能夠在viewDidLoad 方法中加入 textField 爲你自定義輸入框的名稱
  2. [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  3. ...
  4. //第二步,實現回調函數
  5. - (void) textFieldDidChange:(id) sender {
  6.     UITextField *_field = (UITextField *)sender;
  7.     NSLog(@"%@",[_field text]);
  8. }
相關文章
相關標籤/搜索