UITextField與UITextView的區別

 UITextField繼承UIControlapi

     1.文字永遠是一行,不能顯示多行文字數組

     2.有placehoder屬性設置佔位文字less

     3.繼承自UIControlide

     4.監聽行爲函數

     1> 設置代理字體

     2> addTarget:action:forControlEvents:優化

     3> 通知:UITextFieldTextDidChangeNotificationspa

 

 UITextView繼承UIScrollView代理

     1.能顯示任意行文字日誌

     2.不能設置佔位文字

     3.繼承自UIScollView

     4.監聽行爲

     1> 設置代理

     2> 通知:UITextViewTextDidChangeNotification

 

UITextField的詳細使用

1 設置邊框樣式,只有設置了纔會顯示邊框樣式  

  text.borderStyle = UITextBorderStyleRoundedRect;

2 輸入框中是否有個叉號,在何時顯示,用於一次性刪除輸入框中的內容

  text.clearButtonMode = UITextFieldViewModeAlways;
  
  typedef enum {
    UITextFieldViewModeNever,  重不出現
    UITextFieldViewModeWhileEditing, 編輯時出現
    UITextFieldViewModeUnlessEditing,  除了編輯外都出現
    UITextFieldViewModeAlways   一直出現
} UITextFieldViewMode;

3 每輸入一個字符就變成點 用語密碼輸入

text.secureTextEntry = YES;

 

4 是否糾錯

  text.autocorrectionType = UITextAutocorrectionTypeNo;
  
  typedef enum {
    UITextAutocorrectionTypeDefault, 默認
    UITextAutocorrectionTypeNo,   不自動糾錯
    UITextAutocorrectionTypeYes,  自動糾錯
} UITextAutocorrectionType;

 

5 再次編輯就清空

  text.clearsOnBeginEditing = YES;

 

6 內容的垂直對齊方式  

UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment
  
  text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

 

7 設置爲YES時文本會自動縮小以適應文本窗口大小  

  textFied.adjustsFontSizeToFitWidth = YES;
  
  //設置自動縮小顯示的最小字體大小
  text.minimumFontSize = 20;

 

8 設置鍵盤的樣式

  text.keyboardType = UIKeyboardTypeNumberPad;
  
  typedef enum {
    UIKeyboardTypeDefault,       默認鍵盤,支持全部字符         
    UIKeyboardTypeASCIICapable,  支持ASCII的默認鍵盤
    UIKeyboardTypeNumbersAndPunctuation,  標準電話鍵盤,支持+*#字符
    UIKeyboardTypeURL,            URL鍵盤,支持.com按鈕 只支持URL字符
UIKeyboardTypeNumberPad,              數字鍵盤
UIKeyboardTypePhonePad,     電話鍵盤
    UIKeyboardTypeNamePhonePad,   電話鍵盤,也支持輸入人名
UIKeyboardTypeEmailAddress,   用於輸入電子 郵件地址的鍵盤     
UIKeyboardTypeDecimalPad,     數字鍵盤 有數字和小數點
    UIKeyboardTypeTwitter,        優化的鍵盤,方便輸入@、#字符
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 
} UIKeyboardType;

 

9 首字母是否大寫

  text.autocapitalizationType = UITextAutocapitalizationTypeNone;
  
  typedef enum {
    UITextAutocapitalizationTypeNone, 不自動大寫
    UITextAutocapitalizationTypeWords,  單詞首字母大寫
    UITextAutocapitalizationTypeSentences,  句子的首字母大寫
    UITextAutocapitalizationTypeAllCharacters, 全部字母都大寫
} UITextAutocapitalizationType;

 

10 return鍵變成什麼鍵‍

  text.returnKeyType =UIReturnKeyDone;
  
  typedef enum {
    UIReturnKeyDefault, 默認 灰色按鈕,標有Return
    UIReturnKeyGo,      標有Go的藍色按鈕
    UIReturnKeyGoogle,標有Google的藍色按鈕,用語搜索
    UIReturnKeyJoin,標有Join的藍色按鈕
    UIReturnKeyNext,標有Next的藍色按鈕
    UIReturnKeyRoute,標有Route的藍色按鈕
    UIReturnKeySearch,標有Search的藍色按鈕
    UIReturnKeySend,標有Send的藍色按鈕
    UIReturnKeyYahoo,標有Yahoo的藍色按鈕
    UIReturnKeyYahoo,標有Yahoo的藍色按鈕
    UIReturnKeyEmergencyCall, 緊急呼叫按鈕
} UIReturnKeyType;

 

11 鍵盤外觀

textView.keyboardAppearance=UIKeyboardAppearanceDefault;

typedef enum {
UIKeyboardAppearanceDefault, 默認外觀,淺灰色
UIKeyboardAppearanceAlert,     深灰 石墨色
} UIReturnKeyType;

 

 12 最右側加圖片是如下代碼左側相似

    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
    text.rightView=image;
    text.rightViewMode = UITextFieldViewModeAlways; 
    
    typedef enum {
    UITextFieldViewModeNever,
    UITextFieldViewModeWhileEditing,
    UITextFieldViewModeUnlessEditing,
    UITextFieldViewModeAlways
} UITextFieldViewMode;

 

13 按return鍵鍵盤往下收  

[txtField becomeFirstResponder];     [text resignFirstResponder];
[txtField endEditing:YES]; 
[self.view endEditing:YES];

 

14 重寫繪製行爲

除了UITextField對象的風格選項,你還能夠定製化UITextField對象,爲他添加許多不一樣的重寫方法,來改變文本字段的顯示行爲。這些方法都會返回一個CGRect結構,制定了文本字段每一個部件的邊界範圍。如下方法均可以重寫。

– textRectForBounds:      //重寫來重置文字區域

– drawTextInRect:          //改變繪文字屬性.重寫時調用super能夠按默認圖形屬性繪製,若本身徹底重寫繪製函數,就不用調用super了.

– placeholderRectForBounds:  //重寫來重置佔位符區域

– drawPlaceholderInRect:  //重寫改變繪製佔位符屬性.重寫時調用super能夠按默認圖形屬性繪製,若本身徹底重寫繪製函數,就不用調用super了.

– borderRectForBounds:  //重寫來重置邊緣區域

– editingRectForBounds:  //重寫來重置編輯區域

– clearButtonRectForBounds:  //重寫來重置clearButton位置,改變size可能致使button的圖片失真

– leftViewRectForBounds:

– rightViewRectForBounds:

 

15 委託方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  
//返回一個BOOL值,指定是否循序文本字段開始編輯  
    return YES;  
}  
 
- (void)textFieldDidBeginEditing:(UITextField *)textField{  
 //開始編輯時觸發,文本字段將成爲first responder  
}  
 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{  
//返回BOOL值,指定是否容許文本字段結束編輯,當編輯結束,文本字段會讓出first responder  
   //要想在用戶結束編輯時阻止文本字段消失,能夠返回NO  
   //這對一些文本字段必須始終保持活躍狀態的程序頗有用,好比即時消息  
   
    return NO;  
}  
 
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
//當用戶使用自動更正功能,把輸入的文字修改成推薦的文字時,就會調用這個方法。  
//這對於想要加入撤銷選項的應用程序特別有用  
//能夠跟蹤字段內所作的最後一次修改,也能夠對全部編輯作日誌記錄,用做審計用途。     
//要防止文字被改變能夠返回NO  
//這個方法的參數中有一個NSRange對象,指明瞭被改變文字的位置,建議修改的文本也在其中  
 
     return YES;  
}  
 
- (BOOL)textFieldShouldClear:(UITextField *)textField{  
 
//返回一個BOOL值指明是否容許根據用戶請求清除內容  
//能夠設置在特定條件下才容許清除內容  
 
     return YES;  
}  
 
-(BOOL)textFieldShouldReturn:(UITextField *)textField{  
 
//返回一個BOOL值,指明是否容許在按下回車鍵時結束編輯  
 //若是容許要調用resignFirstResponder 方法,這回致使結束編輯,而鍵盤會被收起[textField resignFirstResponder];
//查一下resign這個單詞的意思就明白這個方法了  
     return YES;  
}

 

16 通知

UITextField派生自UIControl,因此UIControl類中的通知系統在文本字段中也能夠使用。除了UIControl類的標準事件,你還能夠使用下列UITextField類特有的事件

UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification

當文本字段退出編輯模式時觸發。通知的object屬性存儲了最終文本。

由於文本字段要使用鍵盤輸入文字,因此下面這些事件發生時,也會發送動做通知

UIKeyboardWillShowNotification   //鍵盤顯示以前發送
UIKeyboardDidShowNotification    //鍵盤顯示以後發送
UIKeyboardWillHideNotification   //鍵盤隱藏以前發送
UIKeyboardDidHideNotification    //鍵盤隱藏以後發送

 

UITextField的運用

1 限制只能輸入特定的字符

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分離出數組,數組按@""分離出字符串
    BOOL canChange = [string isEqualToString:filtered];
    return canChange;
}

 上面那個NUMBERS是一個宏,能夠在文件頂部定義:

#define NUMBERS @」0123456789\n」 (這個表明能夠輸入數字和換行,請注意這個\n,若是不寫這個,Done按鍵將不會觸發,若是用在SearchBar中,將會不觸發Search事件,由於你本身限制不讓輸入\n,好慘,我在項目中才發現的。)

因此,若是你要限制輸入英文和數字的話,就能夠把這個定義爲:

#define kAlphaNum   @」ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。

固然,你還能夠在以上方法return以前,作一提示的,好比提示用戶只能輸入數字之類的。若是你以爲有須要的話。

 

2 限制只能輸入必定長度的字符

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 
{  //string就是此時輸入的那個字符 textField就是此時正在輸入的那個輸入框 返回YES就是能夠改變輸入框的值 NO相反
if ([string isEqualToString:@"\n"])  //按會車能夠改變
    { 
        return YES; 
    } 
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //獲得輸入框的內容
    if (self.myTextField == textField)  //判斷是否時咱們想要限定的那個輸入框
    { 
        if ([toBeString length] > 20) { //若是輸入框內容大於20則彈出警告
            textField.text = [toBeString substringToIndex:20]; 
            UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超過最大字數不能輸入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease]; 
            [alert show]; 
            return NO; 
        } 
    } 
    return YES; 
}

 

3 編輯時候,視圖在鍵盤頂部

self.textField.delegate = self;
#pragma mark - UITextField
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //1.發送監控鍵盤通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

- (void)keyboardDidChangeFrame:(NSNotification *)notification
{    
    //視圖座標變化
    NSDictionary *userInfo = notification.userInfo;
    //動做時間
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //鍵盤彈出結束的frame
    CGRect keyBoardRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    if (CGRectGetMaxY(self.scrollView.frame) >= keyBoardRect.origin.y) {
        [UIView animateWithDuration:duration animations:^{

            self.view.y = keyBoardRect.origin.y - self.view.height + self.viewIndex;
         
        }];
    }
 
    
}
//3. 釋放通知
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
相關文章
相關標籤/搜索