[英]setting the size of UITextView and UITableViewCell depending on Content
[英]Size of UITextView after setting its height depending of its content SWIFT
我有一個UITextView,具有高度限制,以便使框架的高度適應其內容(取決於字符串的長度)。 (我使用自動版式)。 工作正常。
但是,我對textview框架的大小一無所知。
我解釋。
在設置uitextview的文本以及更新約束之后,我將顯示框架信息。
override func viewDidLoad() {
...
self.textview.text = post.title
...
}
override func viewDidLayoutSubviews() {
let contentSize = self.textview.sizeThatFits(self.textview.bounds.size)
self.heightConstraint.constant = contentSize.height
self.textview.layoutIfNeeded()
print(self.textview.frame) // frame
print(self.textview.bounds) // bounds
}
結果:
對於情況A:
(8.0, 0.0, 584.0, 97.0) //frame
(0.0, 0.0, 584.0, 97.0) //bounds
對於情況B:
(8.0, 0.0, 584.0, 97.0) //frame
(0.0, 0.0, 584.0, 97.0) //bounds
我不明白為什么兩種情況下鏡框的高度都一樣???
如果您不需要UITextView
的滾動部分,這將解決問題:
class ViewController: UIViewController {
let textView = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.view.addSubview(self.textView)
self.textView.backgroundColor = UIColor.redColor()
self.textView.text = "Hello world Hello world Hello world Hello world Hello world Hello world"
self.textView.translatesAutoresizingMaskIntoConstraints = false
self.textView.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
self.textView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor).active = true
self.textView.rightAnchor.constraintEqualToAnchor(self.view.rightAnchor).active = true
self.textView.scrollEnabled = false // magic which will help auto expanding
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.