您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 6 控制項專輯:UITextView 完整用法

編輯:iOS 開發綜合

UITextView 是多行文字輸入的常用控制項。本文整理它的建立、樣式設定、自適應高度,以及收合鍵盤的幾種常見做法。

一、建立與初始化

在 .h 中宣告並遵循代理:

#import <UIKit/UIKit.h>

@interface TextViewController : UIViewController <UITextViewDelegate>
@property (nonatomic, weak) UITextView *textView;
@end

在 .m 中初始化並設定各項屬性:

self.textView = [[UITextView alloc] initWithFrame:self.view.frame];
self.textView.textColor = [UIColor blackColor];        // 文字顏色
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];  // 字型與大小
self.textView.delegate = self;                          // 設定代理
self.textView.backgroundColor = [UIColor whiteColor];   // 背景顏色
self.textView.text = @"Now is the time for all good developers...";  // 內容
self.textView.returnKeyType = UIReturnKeyDefault;       // 換行鍵樣式
self.textView.keyboardType = UIKeyboardTypeDefault;     // 鍵盤類型
self.textView.scrollEnabled = YES;                      // 可捲動
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;  // 自適應高度
[self.view addSubview:self.textView];

二、設定圓角

引入 #import <QuartzCore/QuartzCore.h> 後,設定 layer 的圆角半徑:

[textView.layer setCornerRadius:10];

三、依內容自適應高度

可用文字內容配合 sizeWithFont 計算所需高度:

NSString *desc = @"Description it is a test font...";
CGSize size = [desc sizeWithFont:[UIFont systemFontOfSize:14.0f]
               constrainedToSize:CGSizeMake(FLT_MAX, 21.0)
                   lineBreakMode:NSLineBreakByWordWrapping];

四、收起鍵盤的幾種方式

點擊 UITextView 會彈出鍵盤,若要收起,有以下幾種做法:

(1)在導航列放一個 Done 按鈕

有導航列時,進入編輯就在右側放一個 Done 鈕:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                          target:self
                                                                          action:@selector(leaveEditMode)];
    self.navigationItem.rightBarButtonItem = done;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    self.navigationItem.rightBarButtonItem = nil;
}

- (void)leaveEditMode {
    [self.textView resignFirstResponder];
}

(2)把換行鍵當成收起鍵

若 textView 不需要換行,可攔截換行輸入來收起鍵盤,無論用實體鍵盤還是螢幕鍵盤的 return 都有效:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

(3)在鍵盤上方加一條工具列

利用 inputAccessoryView 在鍵盤上方放一個含 Done 的 UIToolbar:

UIToolbar *topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlack];

UIBarButtonItem *helloButton = [[UIBarButtonItem alloc] initWithTitle:@"Hello"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self action:nil];
UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                              style:UIBarButtonItemStyleDone
                                                             target:self action:@selector(dismissKeyBoard)];
topView.items = @[helloButton, btnSpace, doneButton];
[tvTextView setInputAccessoryView:topView];
-(void)dismissKeyBoard {
    [tvTextView resignFirstResponder];
}

以上三種方式各有適用情境:有導航列時用 Done 鈕最簡單;純輸入、不需換行時攔截換行鍵最省事;需要更完整的操作列(例如切換上一個/下一個輸入框)時,自訂 inputAccessoryView 最靈活。要留意的是,UITextView 本身沒有 returnKeyType 的實際送出按鍵(它只有換行),所以「按下 Done 就送出」這類需求,通常都得靠工具列或換行鍵攔截來實現。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved