先看效果图

github源码 github.com/daomoer/YYS… 初始化一个UITextView

let textView = UITextView(frame: CGRect(x:30, y:200, width:UIScreen.main.bounds.width-60, height:200))
textView.delegate = self
self.view.addSubview(textView)
复制代码

UITextView属性设置

//字体颜色
textView.textColor = UIColor.purple
//内容部分链接样式
textView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange,NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]
//边框
textView.layer.borderColor = UIColor.red.cgColor
textView.layer.borderWidth = 1.5
//字体大小
textView.font = UIFont.systemFont(ofSize: 16)
//内容可编辑
textView.isEditable = false
//内容可选
textView.isSelectable = true
//边框圆角设置
textView.layer.masksToBounds = true
textView.layer.cornerRadius = 5.0
//自适应高度
textView.autoresizingMask = UIViewAutoresizing.flexibleHeight
复制代码

设置内容文本富文本样式显示

//设置富文本
let attributeString:NSMutableAttributedString=NSMutableAttributedString(string: "这是一条测试textview的字符串数据,欢迎访问 http://www.baidu.com")
//设置字体颜色
attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.green, range: NSMakeRange(0, attributeString.length))
//文本所有字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, attributeString.length))
//文本0开始5个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: 26)!, range: NSMakeRange(0, 5))
//设置字体颜色
attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.white, range: NSMakeRange(0, 3))
//设置文字背景颜色
attributeString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.orange, range: NSMakeRange(3, 3))
//赋值富文本
textView.attributedText = attributeString
复制代码

设置文本内容对齐方式

textView.textAlignment = NSTextAlignment.center
/*textView.textAlignment = NSTextAlignment.lefttextView.textAlignment = NSTextAlignment.center*/
复制代码

给文中的特殊部分加上链接,比如手机号,网址邮箱等(需要注意的是如果使用了给特殊部分加上连接要先使内容不可编辑textView.isEditable = false)

textView.dataDetectorTypes = UIDataDetectorTypes.link
/*textView.dataDetectorTypes = UIDataDetectorTypes.linktextView.dataDetectorTypes = UIDataDetectorTypes.addresstextView.dataDetectorTypes = UIDataDetectorTypes.calendarEventtextView.dataDetectorTypes = UIDataDetectorTypes.shipmentTrackingNumbertextView.dataDetectorTypes = UIDataDetectorTypes.flightNumbertextView.dataDetectorTypes = UIDataDetectorTypes.lookupSuggestiontextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber*/
复制代码

设置键盘类型

textView.keyboardType = UIKeyboardType.asciiCapable
/*textView.keyboardType = UIKeyboardType.defaulttextView.keyboardType = UIKeyboardType.numbersAndPunctuationtextView.keyboardType = UIKeyboardType.URLtextView.keyboardType = UIKeyboardType.numberPadtextView.keyboardType = UIKeyboardType.phonePadtextView.keyboardType = UIKeyboardType.namePhonePadtextView.keyboardType = UIKeyboardType.emailAddresstextView.keyboardType = UIKeyboardType.decimalPadtextView.keyboardType = UIKeyboardType.twitter*/
复制代码

键盘返回类型

textView.returnKeyType = UIReturnKeyType.continue
/*textView.returnKeyType = UIReturnKeyType.defaulttextView.returnKeyType = UIReturnKeyType.gotextView.returnKeyType = UIReturnKeyType.googletextView.returnKeyType = UIReturnKeyType.jointextView.returnKeyType = UIReturnKeyType.nexttextView.returnKeyType = UIReturnKeyType.routetextView.returnKeyType = UIReturnKeyType.searchtextView.returnKeyType = UIReturnKeyType.sendtextView.returnKeyType = UIReturnKeyType.yahootextView.returnKeyType = UIReturnKeyType.donetextView.returnKeyType = UIReturnKeyType.emergencyCalltextView.returnKeyType = UIReturnKeyType.continue*/
复制代码

当选择内容文本的时候,顶部会出现对选择内容操作的segment,可以选择自定义segment

let mail = UIMenuItem(title: "邮件", action: #selector(ViewController.onMail))
let weixin = UIMenuItem(title: "微信", action: #selector(ViewController.onWeiXin))
let menu = UIMenuController()
menu.menuItems = [mail,weixin]
}
@objc func onMail(){}@objc func onWeiXin(){}
复制代码

下面为UITextView的各个代理方法

// 开始编辑文本的时候触发该方法
func textViewDidBeginEditing(_ textView: UITextView) {}
// 结束编辑的时候触发该方法
func textViewDidEndEditing(_ textView: UITextView) {}//如果返回false,文本视图不能编辑
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {return true
}//如果返回false,表示编辑结束之后,文本视图不可再编辑
func textViewShouldEndEditing(_ textView: UITextView) -> Bool {return true
}//文本视图内容改变时,触发本方法,如果是回车符号,则textView释放第一响应值,返回false
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {if (text ==  "\n") {textView.resignFirstResponder()return false;}return true
}//文本视图改变后触发本代理方法
func textViewDidChange(_ textView: UITextView) {}
//文本视图 改变选择内容,触发本代理方法
func textViewDidChangeSelection(_ textView: UITextView) {}
//链接在文本中显示。当链接被点击的时候,会触发本代理方法
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool{return true
}
//文本视图允许提供文本附件,文本附件点击时,会触发本代理方法   return true
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {return true
}
复制代码

转载于:https://juejin.im/post/5c88e4ea5188251bbf2ee9e3

Swift4 2 UITextView基本用法相关推荐

  1. uiswitch样式_Swift - 表格UITableView的plain、grouped两种样式详解(附分组头悬停)

    在表格 tableview初始化的时候我们可以指定需要使用的 UITableViewStyle样式,可用的样式一共有两种:.plain和 .grouped.下面分别对它们做介绍. 一.plain模式 ...

  2. iPhone开发部分总结

    iphone程序中实现截屏的一种方法 在iphone程序中实现截屏的一种方法: //导入头 文件 #import QuartzCore/QuartzCore.h //将整个self.view大小的图层 ...

  3. iphone开发笔记和技巧总结

    在iphone程序中实现截屏的一种方法: //导入头文件   #importQuartzCore/QuartzCore.h //将整个self.view大小的图层形式创建一张图片imageUIGrap ...

  4. ios学习--iphone开发笔记和技巧总结(原址持续更新)

    ios学习--iphone开发笔记和技巧总结(原址持续更新) 分类: ios Object-C2012-04-18 10:16 2716人阅读 评论(1) 收藏 举报 uiviewiphonelist ...

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

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

  6. UITextView 用法小结

    2019独角兽企业重金招聘Python工程师标准>>> **粗体**// 初始化输入框并设置位置和大小 UITextView *textView = [[UITextView all ...

  7. ios开发读取剪切板的内容_iOS中管理剪切板的UIPasteboard粘贴板类用法详解

    一.自带剪切板操作的原生UI控件在iOS的UI系统中,有3个控件自带剪切板操作,分别是UITextField.UITextView与UIWebView.在这些控件的文字交互处进行长按手势可以在屏幕视图 ...

  8. iOS 之UITextFiled/UITextView小结

    一:编辑被键盘遮挡的问题    (推荐使用第三方库:IQKeyboardManager  用法参考) 参考自:http://blog.csdn.net/windkisshao/article/deta ...

  9. UIWebView、UITextView 和UILable 设置行间距

    01.使用UIWebView调整行距 添加 UIWebView,并对 WebView 应用如下代码: //下面两行协助 UIWebView 背景透明化,这两属性可以在 xib 中进行设置 webvie ...

最新文章

  1. 上如何刻字_如何用简单的方法在零件表面刻字:这里就有你想要的答案
  2. 机器学习知识点(八)感知机模型Java实现
  3. equals属于mysql语句吗_equals和==的区别 (Java基础)
  4. 关于正则表达式,我所知道的都在这里
  5. android webview js 失效,Android WebView注入JQuery、JS脚本及执行无效的问题解决
  6. pytorch 和 tensorflow2.0 方法替换
  7. 模糊匹配查询_必须掌握的6个查询函数应用技巧,办公必备,收藏备用!
  8. Python自然语言处理学习笔记(30):4.2 序列
  9. 粉丝关注数据库表的设计
  10. 全志r16android sdk,全志 Allwinner R16 SoC 全套设计资料分享 原理图 PCB 数据手册 SDK...
  11. 总结了 110+ 公开专业数据集
  12. python分秒换算_如何将度分秒转换为度分
  13. 文献阅读笔记之 - - 48V锂电池管理系统的设计与实现(贾小龙)
  14. 错题集 HDLBits Exams/ece241 2013 q7 JK触发器
  15. FTXUI基础笔记(botton按钮组件基础)
  16. mysql ndb同步_MySQL NDB Cluster 7.5.16 部署OGG同步
  17. 为什么这位俄罗斯亿万富翁要为音乐节和音乐会创造一个虚拟现实世界?
  18. uniapp-自定义导航栏样式不生效问题(一)
  19. 具有自主、多鳍和仿生机器人的鱼类三维游泳(2021)
  20. 二级建造师报考条件不符,选择代报名靠谱吗?

热门文章

  1. matlab熔断器,Resilience4j 熔断器
  2. .gitignore git忽略文件
  3. 用python画小仓鼠教程_彩铅画教程:教你画小仓鼠
  4. H3C 设备自检常用命令
  5. 当你程序运行出现错时~
  6. python实现PCA
  7. 将Windows7屏幕外的窗口拖回
  8. 小霸王推出可挖矿的VR眼镜,把IPFS、以太坊技术全用上了
  9. stackoverflow 查看最新回复
  10. 夜神模拟器设置与PC同一网段,单IP