简介

  • UITextView 具有 label 大部分属性,以及 textField 的属性。

1、UITextView 的创建

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

2、UITextView 的设置

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

3、UITextViewDelegate 的协议方法

  • 需遵守协议 UITextViewDelegate,并设置代理
// 将要开始编辑,编辑开始前被调用
- (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 {}

转载于:https://www.cnblogs.com/CH520/p/9413469.html

UITextView相关推荐

  1. iOS UILabel UITextView自适应文本,或文本大小自适应

    //UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];label ...

  2. iOS UITextView 随键盘弹出界面上移

    - (void)textViewDidBeginEditing:(UITextView *)textView { CGRect frame = textView.frame; int offSet = ...

  3. Swift - 多行文本输入框(UITextView)的用法

    1,多行文本控件的创建 1 2 3 4 var textview=UITextView(frame:CGRectMake(10,100,200,100)) textview.layer.borderW ...

  4. UITextView 取消键盘方法

    首先UITextView 要实现delegate UITextViewDelegate 然后在.m文件里实现该协议: - (void)textViewDidBeginEditing:(UITextVi ...

  5. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  6. UITextView中的占位符

    我的应用程序使用UITextView . 现在,我希望UITextView具有类似于可以为UITextField设置的占位符. 这个怎么做? #1楼 简单的方法,只需使用以下UITextViewDel ...

  7. UITextView左边距为0

    UITextView左边距为0 [TextView.textContainer setLineFragmentPadding:.0]; [TextView.layoutManager setAllow ...

  8. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

  9. UITextField与UITextView的区别

    2019独角兽企业重金招聘Python工程师标准>>> UITextField继承UIControl 1.文字永远是一行,不能显示多行文字 2.有placehoder属性设置占位文字 ...

  10. iOS开发 - UITextView输入时高度自适应

    2019独角兽企业重金招聘Python工程师标准>>> // 设置边距 依次为:上,左,下, 右 textView.textContainerInset = UIEdgeInsets ...

最新文章

  1. 【题解】Luogu P2730 魔板
  2. beacon帧字段结构最全总结(三)——VHT字段总结
  3. PHP(一)——概述及服务器配置
  4. docker安装问题
  5. “约见”面试官系列之常见面试题之第四十九篇之ie6bug的解决办法(建议收藏)
  6. ckedit 3.0 配置(一)
  7. 【AD】如何将喜欢的图案导出成为丝印层和PCB形状
  8. Hive的UDF是什么?
  9. 网规之路——强化项目管理知识点训练
  10. Powershell管理系列(一)Active Direcrtory管理:用户管理
  11. asp.net C#后台实现下载文件的几种方法
  12. usb摄像头做教学直播实现pc和手机都可以在线观看教程
  13. foxpro导入 mysql_Foxpro DBF数据库转换成SQL Server 6.5表的几种方法
  14. k8s cm ( ConfigMap ) 学习
  15. asp.net 生命周期
  16. 如何重装Mac OS系统
  17. 韶音骨传导耳机好用吗?韶音和南卡骨传导耳机对比实测
  18. 求a100的值,an是一个集合{2^s+2^t且t>s>=0}程序验证
  19. 协作通信-af df的matlab仿真,协作通信-AF与DF的MATLAB仿真.pdf
  20. Python中 -m pip install -U --force-reinstall pip 个人经验及解决办法

热门文章

  1. mongo系统(1)
  2. EDEN-MACE 1.4.0 更新,增加数据清理功能
  3. SpringFramework之javax.servlet.http.HttpSession
  4. Android Lint 实践 —— 简介及常见问题分析
  5. 记几种解决跨域问题的办法
  6. HTML:调用静态页面html 的几种方法
  7. 5.7 Components — Sending Actions From Components to Your Application
  8. JS Bin Tips and Bits • About
  9. HDU 1002 大整数
  10. Android@Home与智能家居