UITextView
Usage
// 多行文本控件的创建
let textView = UITextView(frame:CGRect(x:10, y:100, width:200, height:100))
textView.layer.borderWidth = 1 //边框粗细
textView.layer.borderColor = UIColor.gray.cgColor //边框颜色
self.view.addSubview(textView)
// 是否可编辑
textView.isEditable = false
// 内容是否可选
textView.isSelectable = false
// 给文字中的电话号码和网址自动加链接
textView.dataDetectorTypes = [] //都不加链接
textView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber //只有电话加链接
textView.dataDetectorTypes = UIDataDetectorTypes.link //只有网址加链接
textView.dataDetectorTypes = UIDataDetectorTypes.all //电话和网址都加
// 自定义选择内容后的菜单
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let textView = UITextView(frame:CGRect(x:10, y:100, width:200, height:100))
textView.layer.borderWidth = 1 //边框粗细
textView.layer.borderColor = UIColor.gray.cgColor //边框颜色
self.view.addSubview(textview)
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(){
print("mail")
}
@objc func onWeiXin(){
print("weixin")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
// 获取选择的文本
guard let textRange = textView.selectedTextRange else { return }
let selectedText = textView.text(in: textRange)