iOS UI开发之UITextView swift版本

作者: littleGG 发布时间: 2019-08-20 浏览: 2332 次 编辑

前言

  • UITextView可滚动的多行文本区域

  • UITextView支持使用自定义样式信息显示文本,并支持文本编辑。您通常使用文本视图来显示多行文本,例如在显示大型文本文档的正文时。

  • UITextView继承于UIScrollView

属性和方法

初始化方法

let textView = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 200))


设置文本内容

textView.text = "文本"


设置文本的字体

textView.font = UIFont.systemFont(ofSize: 15.0)


设置文本的颜色

textView.textColor = UIColor.red


设置是否可以编辑,默认是YES

textView.isEditable = true


设置文本的对齐方式

 textView.textAlignment = .center


UITextView代理方法

即将开始编辑

optional public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool


已经开始编辑

optional public func textViewDidBeginEditing(_ textView: UITextView)


编辑即将结束

optional public func textViewShouldEndEditing(_ textView: UITextView) -> Bool


编辑已经结束

optional public func textViewDidEndEditing(_ textView: UITextView)


文本视图在用户输入新字符或删除现有字符时调用此方法

optional public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool


输入的内容已经变化时调用此方法

optional public func textViewDidChange(_ textView: UITextView)


补充说明

UITextView相关通知

UITextView相关通知名称说明

textDidBeginEditingNotification:

TextView开始编辑时发出的通知

textDidChangeNotification:

TextView的内容开始改变时发出的通知

textDidEndEditingNotification:

TextView结束编辑时发出的通知

UITextViewdataDetectorTypes属性对应的枚举以及说明

类型说明

phoneNumber

检测格式化为电话号码的字符串。

link

检测格式为URL的字符串。

address

检测格式为地址的字符串。

calendarEvent

检测格式化为日历事件的字符串。

shipmentTrackingNumber

检测格式化为包裹递送公司的跟踪号码的字符串。

flightNumber

检测格式化为航空公司航班号的字符串。

lookupSuggestion

检测格式化为用户可能要查找的信息的字符串。


all

接收所有类型的字符串