私はサブビューとしてUITextView
を持ち、セルの境界に固定されているUICollectionViewCell
を持っています。計算UICollectionViewCell高さベースドンUITextView帰属テキスト
UITextView attributedString
に基づいて達成しようとしているのは、UITextView
の高さを計算し、スクロールしないようにしてセルの高さにそのサイズを適用することです。
ここで問題となるのは、NSAttributedString
のフォントサイズが異なる可能性があり、画像サイズが大きくなることがあります。
複数の内部フォーム(サイズ、改行、イメージ)を持つことができるそのコンテンツに基づいて、サイズを計算するにはどうすればよいですか?
私は次の関数を適用するために使用しますが、それはこのケースでは動作しません
、内側のコンテンツので、フォントサイズが延期するように、それは一定ではない。private func estimatedHeightForText(_ text: String) -> CGFloat {
let approximateWidthOfBioTextView = view.frame.width - 24 // paddings
let size = CGSize(width: approximateWidthOfBioTextView, height: 1000) // height of 1000 is an arbitrary value
let attributes = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)] // attributes of the text
// gets the estimation height based on Text
let estimatedFrame = NSString(string: text).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
return estimatedFrame.height
}
任意のヒント?
ありがとうございました。