1.描述
A scrollable, multiline text region.(一个多行的可滚动的文本区域)
class UITextView : UIScrollView
我们可以理解其为多行的UITextField,其特性基本与其相似所以这里不多加赘述.
2.代码示例
let myTextView1 = UITextView()
self.view.addSubview(myTextView1)
myTextView1.backgroundColor = UIColor.lightGray
myTextView1.translatesAutoresizingMaskIntoConstraints = false
// let constrantArrayH = NSLayoutConstraint.constraints(withVisualFormat: "H:|-100-[tv(200)]", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["tv" : myTextView1])
let constrantArrayH = NSLayoutConstraint.init(item: myTextView1, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 100)
let constrantArrayH1 = NSLayoutConstraint.init(item: myTextView1, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.width, multiplier: 0, constant: 200)
let constrantArrayW = NSLayoutConstraint.constraints(withVisualFormat: "V:|-100-[tv(200)]", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["tv" : myTextView1])
// self.view.addConstraint(constrantArrayH)
// self.view.addConstraint(constrantArrayH1)
self.view.addConstraints([constrantArrayH,constrantArrayH1])
self.view.addConstraints(constrantArrayW)
let attributedString = NSMutableAttributedString.init(string: "富文本---内容")
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange.init(location: 0, length: 2))
attributedString.addAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20),NSAttributedString.Key.backgroundColor : UIColor.red], range: NSRange.init(location: 2, length: 1))
myTextView1.attributedText = attributedString
- 工程下载地址:
https://github.com/DeveloperZhang/SwiftStudyDemo
3.总结
UITextField是一个最基础常见的视图类,可以参考文档进行深入学习:UIKit->Views and Controls->UITextView
网友评论