Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 估计某些文本的UICollectionView单元格的大小_Swift_Uicollectionview_Uitextview - Fatal编程技术网

Swift 估计某些文本的UICollectionView单元格的大小

Swift 估计某些文本的UICollectionView单元格的大小,swift,uicollectionview,uitextview,Swift,Uicollectionview,Uitextview,因此,我正在创建一个消息类型的应用程序,它由一些包含不同长度文本的UITextView块组成,这些块位于一个“气泡”中UIView let textView: UITextView = { let text = UITextView() text.text = "SAMPLE" text.translatesAutoresizingMaskIntoConstraints = false text.backgroundColor = .clear text.

因此,我正在创建一个消息类型的应用程序,它由一些包含不同长度文本的
UITextView
块组成,这些块位于一个“气泡”中
UIView

let textView: UITextView = {
    let text = UITextView()
    text.text = "SAMPLE"
    text.translatesAutoresizingMaskIntoConstraints = false
    text.backgroundColor = .clear
    text.textColor = .white
    return text
}()
let bubbleView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor(r: 0, g: 137, b: 247)
    view.layer.cornerRadius = 14
    view.layer.masksToBounds = true
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()
var bubbleWidthAnchor: NSLayoutConstraint?

    bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8).isActive = true
    bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 250)
    bubbleWidthAnchor?.isActive = true
    bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true

    textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 8).isActive = true
    textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor).isActive = true
    textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
为了设置单元格的高度,我使用了一个自定义函数,该函数应该不能正常工作

自定义功能:

private func estimatedFrameForText(text: String) -> CGRect {
    let size = CGSize(width: 250, height: 250)
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)

    return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16)], context: nil)
}
我在
UICollectionView
sizeForItemAt
函数中调用它:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    var height: CGFloat = 80 //Arbitrary number
    if let text = messages[indexPath.item].text {
        height = estimatedFrameForText(text: text).height + 8
    }
    return CGSize(width: view.frame.width, height: height)
}
我遇到的一个简单问题是。。。它不太管用:


你知道我哪里出错了吗,或者根据文本获得我需要的单元格估计大小的更好解决方案吗?

事实证明,我所缺少的只是在
文本视图中设置文本大小

let textView: UITextView = {
    let text = UITextView()
    text.text = "SAMPLE"
    text.translatesAutoresizingMaskIntoConstraints = false
    text.backgroundColor = .clear
    text.textColor = .white
    return text
}()
let bubbleView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor(r: 0, g: 137, b: 247)
    view.layer.cornerRadius = 14
    view.layer.masksToBounds = true
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()
var bubbleWidthAnchor: NSLayoutConstraint?

    bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8).isActive = true
    bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 250)
    bubbleWidthAnchor?.isActive = true
    bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true

    textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 8).isActive = true
    textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor).isActive = true
    textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
需要将
text.font=UIFont.systemFont(of大小:16)
放入,因为获取估计大小的函数具有:

attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16)

因此,您必须将两者定义为相同。

Swift 4.2更新后的答案是在uilabel的基础上处理uicollectionviewCell的高度和宽度

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
  let size = (self.FILTERTitles[indexPath.row] as NSString).size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)])
    return CGSize(width: size.width + 38.0, height: size.height + 25.0)


}