哪種方法既可以將UITextView
高度適配到其內容,又可以將作爲容器的UITableViewCell
高度調整爲與UITextView
相同的高度?Swift:如何使UITableViewCell高度適應其內部的UITextView
1
A
回答
2
一套符合你的UITextView的數量爲0。 在控制器中包含的tableView使用viewDidLoad中:
tableView.estimatedRowHeight = 55.0
tableView.rowHeight = UITableViewAutomaticDimension
2
要計算你的TextView的高度,我想你需要知道的高度文本。所以,在你的heightForRowAtIndexPath委託方法,計算文本高度和使用其返回此:
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.max)
let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
您需要設置TextView的約束,底部,頂部,開頭和結尾的爲0,所以他們適應的大小細胞。 該代碼被拍下from here。
相關問題
- 1. 如何讓UITableViewCell調整高度以適應其內容?
- 2. 動態UITableViewCell高度內的動態UITextView?
- 3. 如何讓div部分的高度自動適應其內容的高度?
- 4. 如何將UITableViewCell高度調整爲裏面的UITextView的內容?
- 5. 同時增加uitableviewcell高度增加內部UITextView
- 6. 如何設置父div高度自適應內部div高度?
- 7. 如何使UITextView在部分標題中將其高度調整爲其內容
- 8. Swift - 根據UITextView佈局約束的UITableViewCell高度
- 9. UITextView在UITableViewCell內部生長
- 10. 如何在可變高度的UITableViewCell中使用UITextView?
- 11. 如何設置UITableViewCell高度動態地使其內容大小適合?
- 12. 更改動態UITextView的高度內的UITableViewCell
- 13. UITextView的新線autogrowing內部的UITableViewCell
- 14. Java - 如何設置JTextArea高度以適應其內容?
- 15. 如何設置UITableViewcell內部的標籤高度?
- 16. 如何更新UITableViewCell高度,因爲文本輸入到UITextView
- 17. iOS 9 Swift,基於內容的UITextView高度
- 18. Swift:自動調整單元格內的高度UITextView
- 19. 如何調整UITableViewCell的大小以適應其內容?
- 20. 如何使用換行符(\ n)獲取UITextView的內容高度?
- 21. 如何在添加項目時使GridView適應其高度
- 22. iOS/Swift:與UITableViewCell高度問題
- 23. UITableViewCell高度動態子視圖swift
- 24. resignfirstresponders在UITableViewCell內的UITextView
- 25. Div高度以適合其內容
- 26. 如何動畫UITableViewCell高度?
- 27. 如何使用代碼動態更改uitextview內的uitableviewcell的高度而不使用自動佈局?
- 28. 如何使div的高度變化以適應其中包含的其他div?
- 29. 如何使窗口高度適應內容?
- 30. 如何使iframe調整高度以適應內容
如果您的文字很大,您可能必須使用高於55的'estimatedRowHeight'。 – Koen
是的,這個值取決於你在UITableViewCell中擁有的值 – Ro22e0