// 初始化輸入框並設置位置和大小
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;
// 自定義文本框placeholder
tip = [[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 = @"";
}
}
本文固定鏈接:http://www.itechzero.com/ios-development-series-four-uitextview-usage-summary.html,轉載請註明出處。