1. 程式人生 > >IOS開發經常用到齊全控制元件

IOS開發經常用到齊全控制元件

    8. UITextField控制元件
(1) //初始化textfield並設定位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
(2) //設定邊框樣式,只有設定了才會顯示邊框樣式 
text.borderStyle = UITextBorderStyleRoundedRect;
 typedef enum {
        UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
    } UITextBorderStyle;
  (3) //設定輸入框的背景顏色,此時設定為白色 如果使用了自定義的背景圖片邊框會被忽略掉    text.backgroundColor = [UIColor whiteColor]; (4) //設定背景 注意: 只有在 UITextBorderStyleNone 樣式下,設定背景圖才會生效,且圖片大小小於 text 的frame時,圖片會拉伸 text.background = [UIImage imageNamed:@"dd.png"]; (5) //設定背景 // 設定enable為NO 時的背景圖片 text.disabledBackground = [UIImage imageNamed:@"cc.png"
]; (6) //當輸入框沒有內容時,水印提示 提示內容為password text.placeholder = @"password"; (7) //設定輸入框內容的字型樣式和大小 text.font = [UIFont fontWithName:@"Arial" size:20.0f]; (8) //設定字型顏色 text.textColor = [UIColor redColor]; (9) //輸入框中是否有個叉號,在什麼時候顯示,用於一次性刪除輸入框中的內容 text.clearButtonMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever,
 重不出現 UITextFieldViewModeWhileEditing, 編輯時出現 UITextFieldViewModeUnlessEditing, 除了編輯外都出現 UITextFieldViewModeAlways  一直出現 } UITextFieldViewMode; (10) //輸入框中一開始就有的文字 text.text = @"一開始就在輸入框的文字"; (11) //每輸入一個字元就變成點 用語密碼輸入 text.secureTextEntry = YES; (12) //是否糾錯 text.autocorrectionType = UITextAutocorrectionTypeNo; typedef enum { UITextAutocorrectionTypeDefault, 預設 UITextAutocorrectionTypeNo,  不自動糾錯 UITextAutocorrectionTypeYes, 自動糾錯 } UITextAutocorrectionType; (14) //再次編輯就清空 text.clearsOnBeginEditing = YES; (15) //內容對齊方式 text.textAlignment = UITextAlignmentLeft; (16) //內容的垂直對齊方式 /* 內容對齊方式 內容的垂直對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment */ _textField.textAlignment = UITextAlignmentLeft; text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; (17) //設定為YES時文字會自動縮小以適應文字視窗大小.預設是保持原來大小,而讓長文字滾動  textFied.adjustsFontSizeToFitWidth = YES; (18) /* 設定為YES時文字會自動縮小以適應文字視窗大小.預設是保持原來大小,而讓長文字滾動 */ _textField.adjustsFontSizeToFitWidth = YES; //設定自動縮小顯示的最小字型大小,adjustsFontSizeToFitWidth為YES才會起作用 text.minimumFontSize = 20; (19) //設定鍵盤的樣式 text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault,  預設鍵盤,支援所有字元 UIKeyboardTypeASCIICapable, 支援ASCII的預設鍵盤 UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支援+*#字元 UIKeyboardTypeURL, URL鍵盤,支援.com按鈕 只支援URL字元 UIKeyboardTypeNumberPad,  數字鍵盤 UIKeyboardTypePhonePad,   電話鍵盤 UIKeyboardTypeNamePhonePad,  電話鍵盤,也支援輸入人名 UIKeyboardTypeEmailAddress,  用於輸入電子 郵件地址的鍵盤 UIKeyboardTypeDecimalPad,  數字鍵盤 有數字和小數點 UIKeyboardTypeTwitter,  優化的鍵盤,方便輸入@、#字元 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;