UITextView 多行文本控件

前言

  • Objective-C

    1
    NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextInput>
  • Swift

    1
    @available(iOS 2.0, *)		 public class UITextView : UIScrollView, UITextInput
  • UITextView 具有 label 大部分属性,以及 textField 的属性。

1、UITextView 的创建

  • Objective-C

    1
    2
    3
    4
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width - 20, 400)];

    // 将 textView 添加到 view
    [self.view addSubview:textView];
  • Swift

    1
    2
    3
    4
    let textView: UITextView = UITextView(frame: CGRectMake(10, 30, self.view.frame.size.width - 20, 400))

    // 将 textView 添加到 view
    self.view.addSubview(textView)

2、UITextView 的设置

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    // 设置背景颜色
    textView.backgroundColor = [UIColor lightGrayColor];

    // 设置文本
    /*
    由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑
    */
    textView.text = @"写代码快乐\n15911110524\n写代码快乐\nhttps://www.baidu.com\n写代码快乐";

    // 设置对齐方式
    /*
    NSTextAlignmentLeft = 0, // Visually left aligned
    NSTextAlignmentCenter = 1, // Visually centered
    NSTextAlignmentRight = 2, // Visually right aligned
    NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural = 4, // Indicates the default alignment for script
    */
    textView.textAlignment = NSTextAlignmentLeft;

    // 设置文本颜色
    textView.textColor = [UIColor redColor];

    // 设置字体
    textView.font = [UIFont boldSystemFontOfSize:20];

    // 对文本中的电话和网址自动加链接
    /*
    UIDataDetectorTypePhoneNumber = 1 << 0, // Phone number detection
    UIDataDetectorTypeLink = 1 << 1, // URL detection
    UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // Street address detection
    UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // Event detection
    UIDataDetectorTypeNone = 0, // No detection at all
    UIDataDetectorTypeAll = NSUIntegerMax // All types
    */
    textView.dataDetectorTypes = UIDataDetectorTypeAll;

    // 禁止编辑
    /*
    设置为只读,不再能输入内容
    */
    textView.editable = NO;

    // 禁止选择
    /*
    禁止选中文本,此时文本也禁止编辑
    */
    textView.selectable = NO;

    // 禁止滚动
    textView.scrollEnabled = NO;

    // 获取当前选择的范围
    NSRange range = textView.selectedRange;

    // 设置可以对选中的文字加粗
    /*
    选中文字时可以对选中的文字加粗
    */
    textView.allowsEditingTextAttributes = YES;

    // 设置 textView 的代理,需遵守协议 <UITextViewDelegate>
    textView.delegate = self;
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    // 设置背景颜色
    textView.backgroundColor = UIColor.lightGrayColor()

    // 设置文本
    /*
    由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑
    */
    textView.text = "写代码快乐\n15911110524\n写代码快乐\nhttps://www.baidu.com\n写代码快乐"

    // 设置文本颜色
    textView.textColor = UIColor.redColor()

    // 设置对齐方式
    /*
    case Left // Visually left aligned
    case Center // Visually centered
    case Right // Visually right aligned
    case Justified // Fully-justified. The last line in a paragraph is natural-aligned.
    case Natural // Indicates the default alignment for script
    */
    textView.textAlignment = .Left

    // 设置字体
    textView.font = UIFont.systemFontOfSize(20)

    // 对文本中的电话和网址自动加链接
    /*
    PhoneNumber // Phone number detection
    Link // URL detection
    Address // Street address detection
    CalendarEvent // Event detection
    None // No detection at all
    All // All types
    */
    textView.dataDetectorTypes = .All

    // 禁止编辑
    /*
    设置为只读,不再能输入内容
    */
    textView.editable = false

    // 禁止选择
    /*
    禁止选中文本,此时文本也禁止编辑
    */
    textView.selectable = false

    // 禁止滚动
    textView.scrollEnabled = false

    // 获取当前选择的范围
    let range: NSRange = textView.selectedRange

    // 设置可以对选中的文字加粗
    /*
    选中文字时可以对选中的文字加粗
    */
    textView.allowsEditingTextAttributes = true

    // 设置 textView 的代理,需遵守协议 UITextViewDelegate
    textView.delegate = self

3、UITextViewDelegate 的协议方法

  • 需遵守协议 UITextViewDelegate,并设置代理

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    // 将要开始编辑,编辑开始前被调用
    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    return YES;
    }

    // 已经开始编辑,编辑开始后被调用
    - (void)textViewDidBeginEditing:(UITextView *)textView {

    }

    // 将要结束编辑,编辑结束前被调用
    - (BOOL)textViewShouldEndEditing:(UITextView *)textView {

    return YES;
    }

    // 已经结束编辑,编辑结束后被调用
    - (void)textViewDidEndEditing:(UITextView *)textView {

    NSLog(@"编辑的内容是 : %@", textView.text);
    }

    // 文本修改,文本修改前被调用
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    return YES;
    }

    // 文本变更,文本变更时被调用,每输入一个字符时都会被调用
    - (void)textViewDidChange:(UITextView *)textView {

    }

    // 游标移动,选择范围发生变化时被调用
    - (void)textViewDidChangeSelection:(UITextView *)textView {

    }
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    // 将要开始编辑,编辑开始前被调用
    func textViewShouldBeginEditing(textView: UITextView) -> Bool {

    return true
    }

    // 已经开始编辑,编辑开始后被调用
    func textViewDidBeginEditing(textView: UITextView) {

    }

    // 将要结束编辑,编辑结束前被调用
    func textViewShouldEndEditing(textView: UITextView) -> Bool {

    return true
    }

    // 已经结束编辑,编辑结束后被调用
    func textViewDidEndEditing(textView: UITextView) {

    print("编辑的内容是 : \(textView.text)")
    }

    // 文本修改,文本修改前被调用
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    return true
    }

    // 文本变更,文本变更时被调用,每输入一个字符时都会被调用
    func textViewDidChange(textView: UITextView) {

    }

    // 游标移动,选择范围发生变化时被调用
    func textViewDidChangeSelection(textView: UITextView) {

    }
文章目录
  1. 1. 前言
  2. 2. 1、UITextView 的创建
  3. 3. 2、UITextView 的设置
  4. 4. 3、UITextViewDelegate 的协议方法
隐藏目录