1, creation of multi-line text controls
| 1234 |
lettextview = 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) |
2, whether it can be edited
| 1 |
textview.isEditable = false |
3, whether the content is optional
| 1 |
textview.isSelectable = false |
4, attribute font set font, TextColor set font color, TextAlignment set alignment 5, give text to phone number and URL automatically link
| 1234 |
textview.dataDetectorTypes = [] //都不加链接textview.dataDetectorTypes = UIDataDetectorTypes.phoneNumber //只有电话加链接textview.dataDetectorTypes = UIDataDetectorTypes.link //只有网址加链接textview.dataDetectorTypes = UIDataDetectorTypes.all //电话和网址都加 |
6, customize the selection after the menu when we are watching news or novels, often after the click on the text will pop-up menu to select, copy and so on. We can add some additional content to this menu, such as the "Mail" "" button options
| 123456789101112131415161718192021222324252627282930 |
importUIKitclassViewController: UIViewController{ overridefuncviewDidLoad() { super.viewDidLoad() lettextview = 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) letmail = UIMenuItem(title: "邮件", action: #selector(ViewController.onMail)) letweixin = UIMenuItem(title: "", action: #selector(ViewController.onWeiXin)) letmenu = UIMenuController() menu.menuItems = [mail,weixin] } funconMail(){ print("mail") } funconWeiXin(){ print("weixin") } overridefuncdidReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
PS: Apple official website Api:uitextview
Swift-Multiline text input box (Uitextview)