前言

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

1、UITextView 的创建

  • Objective-C

    	UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width - 20, 400)];
    
        // 将 textView 添加到 view
    [self.view addSubview:textView];
  • Swift

    	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

    	// 设置背景颜色
    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

    	// 设置背景颜色
    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

        // 将要开始编辑,编辑开始前被调用
    - (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

    	// 将要开始编辑,编辑开始前被调用
    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) { }

iOS - UITextView的更多相关文章

  1. iOS UITextView 输入内容实时更新cell的高度

    iOS UITextView 输入内容实时更新cell的高度 2014-12-26 11:37 编辑: suiling 分类:iOS开发 来源:Vito Zhang'blog  11 4741 UIT ...

  2. IOS UITextView自适应高度

    LOFTER app需要实现了一个类似iPhone短信输入框的功能,它的功能其实蛮简单,就是:[UITextView的高度随着内容高度的变化而变化].实现思路应该是: 在UITextView的text ...

  3. IOS UITextView支持输入、复制、粘贴、剪切自定义表情

    UITextView是ios的富文本编辑控件,除了文字还可以插入图片等.今天主要介绍一下UITextView对自定义表情的处理. 1.首先识别出文本中的表情文本,然后在对应的位置插入NSTextAtt ...

  4. iOS - UITextView实现placeHolder占位文字

      iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...

  5. iOS UITextView 根据输入text自适应高度

    转载自:http://www.cnblogs.com/tmf-4838/p/5380495.html #import "ViewController.h" @interface V ...

  6. iOS UITextView 设置 NSLinkAttributeName 属性,点击链接跳转

    @interface ViewController ()<UITextViewDelegate> - (void)viewDidLoad{    [super viewDidLoad];  ...

  7. iOS - UITextView放在自定义cell里面-自适应高度

    textView放在自定义cell里面-自适应高度 1,textView有个属性 scrollEnabled  要设置为NO; 2,设置tableview的时候  添加这两行代码: self.tabl ...

  8. iOS - UITextView在调用textViewDidChange方法,九宫格相关中文输入的问题

    问题一 iOS textView在调用 UITextViewDelegate 的 textViewDidChange方法,九宫格相关中文输入的问题 有时候,需要在textViewDidChange处理 ...

  9. 一步一步学ios UITextView(多行文本框)控件的用法详解(五5.8)

    本文转载至 http://wuchaorang.2008.blog.163.com/blog/static/48891852201232014813990/     1.创建并初始化 创建UIText ...

随机推荐

  1. [转]微软SerialPort秘籍[SerialPort为什么死锁程序的分析]

    既然是秘籍,显然是写一些大家不常找到的,MSDN里遗漏提示大家注意的东西. 用过.net 2.0中,自带SerialPort的人,大多都遇到过.莫名其妙的执行Close的时候会死掉的问题.而Wince ...

  2. React-Native入门指导之iOS篇

    React-Native 入门指导系列教程目录 一.准备工作 (已完成) 二.项目介绍与调试 三.CSS样式与Flex布局 四.常用UI控件的使用 五.JSX在React-Native中的应用 六.事 ...

  3. 1. python中的随机函数

         本系列不会对python语法,理论作详细说明:所以不是一个学习教材:详细查考Vamei 大神:通俗易懂:是一个很好(基础-中级-高级)的学习教程.而这里只是我一个学习python的某些专题的 ...

  4. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. Linux软件的安装与卸载

    configure作用:是源码安装软件时配置环境用的 他根据你的配置选项和你的系统情况生成makefile文件 为make 做准备 最常用的参数: ./configure --prefix 作用: 不 ...

  6. 基于clahe的图像去雾

    基于clahe的图像去雾     通过阅读一些资料,我了解到clahe算法对图像去雾有所价值,正好opencv中有了实现,拿过来看一看.   但是现在实现的效果还是有所差异 #);    clahe] ...

  7. 测试-Animator的“当前剪辑CurrentAnimatorStateInfo”

    左边是Transport到Idle,右边是完全到Idle 当进入过渡时,事实上Transport状态并未结束,通过下面打印出来的hash可以看出还是Transport的状态 过渡结束才正式变为Idle ...

  8. IoC容器概述

    IoC(Inverse of Control: 控制反转)是spring容器的内核, 字面意思是: 控制反转, 包含两个内容:(1) 控制, (2) 反转.那到底是什么东西的控制被反转了呢? 对于软件 ...

  9. 乘号在python中的用法,用乘号将元素重复在列表中

    #里面:>>> a=['*5] >>> a ['] >>> a=['0,'*5] >>> a ['0,0,0,0,0,'] #外 ...

  10. HDU 4651 Partition 整数划分,可重复情况

    Partition Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...