iOS開發系列之四 - UITextView 用法小結
// 初始化輸入框並設定位置和大小UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 180)];// 設定預設文本textView.text = @"";// 設定文本字型textView.font = [UIFont fontWithName:@"Arial" size:16.5f];// 設定文本顏色textView.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0f];// 設定文字框背景顏色textView.backgroundColor = [UIColor colorWithRed:254/255.0f green:254/255.0f blue:254/255.0f alpha:1.0f];// 設定文本對齊textView.textAlignment = NSTextAlignmentLeft;// iOS7中文本對齊有以下幾種:// enum {// NSTextAlignmentLeft = 0, 靠左對齊,預設// NSTextAlignmentCenter = 1, 置中對齊// NSTextAlignmentRight = 2, 靠右對齊// NSTextAlignmentJustified = 3, 在一個段落的最後一行自然對齊// NSTextAlignmentNatural = 4, 預設對齊// } NSTextAlignment;// 設定自動錯誤修正方式textView.autocorrectionType = UITextAutocorrectionTypeNo;// 自動錯誤修正方式有以下幾種:// enum {// UITextAutocorrectionTypeDefault, 預設// UITextAutocorrectionTypeNo, 不自動錯誤修正// UITextAutocorrectionTypeYes, 自動錯誤修正// } UITextAutocorrectionType;// 設定自動大寫方式textView.autocapitalizationType = UITextAutocapitalizationTypeNone;// 自動大寫方式有以下幾種:// enum {// UITextAutocapitalizationTypeNone, 不自動大寫// UITextAutocapitalizationTypeWords, 單字首大寫// UITextAutocapitalizationTypeSentences, 句子的首字母大寫// UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫// } UITextAutocapitalizationType;// 設定鍵盤的樣式textView.keyboardType = UIKeyboardTypeDefault;// 鍵盤樣式有以下幾種:// enum {// UIKeyboardTypeDefault, 預設鍵盤,支援所有字元// UIKeyboardTypeASCIICapable, 支援ASCII的預設鍵盤// UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支援+*#字元// UIKeyboardTypeURL, 只支援URL字元的URL鍵盤,支援.com按鈕// UIKeyboardTypeNumberPad, 數字鍵台// UIKeyboardTypePhonePad, 電話鍵盤// UIKeyboardTypeNamePhonePad, 支援輸入人名的電話鍵盤// UIKeyboardTypeEmailAddress, 電子郵件鍵盤// UIKeyboardTypeDecimalPad, 有數字和小數點的數字鍵台// UIKeyboardTypeTwitter, 最佳化的鍵盤,方便輸入@、#字元// UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,// } UIKeyboardType;// 設定return鍵樣式textView.returnKeyType = UIReturnKeyDefault;// return鍵有以下幾種樣式:// enum {// UIReturnKeyDefault, 預設,灰色按鈕,標有Return// UIReturnKeyGo, 標有Go的藍色按鈕// UIReturnKeyGoogle, 標有Google的藍色按鈕,用於搜尋// UIReturnKeyJoin, 標有Join的藍色按鈕// UIReturnKeyNext, 標有Next的藍色按鈕// UIReturnKeyRoute, 標有Route的藍色按鈕// UIReturnKeySearch, 標有Search的藍色按鈕// UIReturnKeySend, 標有Send的藍色按鈕// UIReturnKeyYahoo, 標有Yahoo的藍色按鈕// UIReturnKeyYahoo, 標有Yahoo的藍色按鈕// UIReturnKeyEmergencyCall, 緊急電話按鈕// } UIReturnKeyType;// 設定是否可以拖動textView.scrollEnabled = YES;// 設定代理textView.delegate = self;// 自訂文字框placeholdertip = [[UILabel alloc] initWithFrame:CGRectMake(16, 14, 320, 25)];tip.text = @"您的意見是我們前進的最大動力,謝謝!";tip.font = [UIFont fontWithName:@"Arial" size:16.5f];tip.backgroundColor = [UIColor clearColor];tip.enabled = NO;// 自訂文字框字數統計count = [[UILabel alloc] initWithFrame:CGRectMake(270, 170, 35, 20)];count.text = @"240";count.textAlignment = NSTextAlignmentRight;count.font = [UIFont fontWithName:@"Arial" size:15.0f];count.backgroundColor = [UIColor clearColor];count.enabled = NO;// 顯示文字框及相關控制項[self.view addSubview:feedback];[self.view addSubview:tip];[self.view addSubview:count];// 限制輸入文本長度- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if (range.location < 240) { return YES; } else { return NO; }}// 自訂文字框placeholder- (void)textViewDidChange:(UITextView *)textView{ count.text = [NSString stringWithFormat:@"%d", 240 - feedback.text.length]; if (textView.text.length == 0) { tip.text = @"您的意見是我們前進的最大動力,謝謝!"; } else { tip.text = @""; }}