UITextView的boundingRect无法正常工作

时间:2017-08-30 05:33:56

标签: ios swift uicollectionview uicollectionviewcell

我目前在UITextField上有以下扩展名来计算给定字符串的边界矩形。

func widthHeight(font: UIFont) -> CGRect {
    let constraintRect = CGSize(width: 200, height: 1000)
    let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    return boundingBox
}

constraintRect的宽度是我想要允许的最大宽度。

我像这样设置值和单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuse, for: indexPath) as? ChatCollectionViewCell {

        let text = self.chatLog[indexPath.row].text
        cell.chatTextView.text = text

        cell.chatViewWidth = (text?.widthHeight(font: UIFont.systemFont(ofSize: 16)).width)!

        return cell
    }
    return UICollectionViewCell()
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if let text = self.chatLog[indexPath.row].text {            
        let box = text.widthHeight(font: UIFont.systemFont(ofSize: 16))
        return CGSize(width: view.frame.width, height: box.height + 10)
    }
    return CGSize(width: self.view.frame.width, height: 60)
}

当此代码运行时,我会大量计算错误的单元格大小: enter image description here

正如您所看到的,视图的帧非常混乱。 第一行是“Heya”,第二行是“到目前为止生活如何”,第三行是“我是订书机,你是教科书”。有些细胞太窄,有些细胞太宽。

这是我的自定义collectionViewCell的一些额外代码:

override init(frame: CGRect) {
    super.init(frame: frame)

    setupViews()
}

override func layoutSubviews() {

    chatView.frame = CGRect(x: 0, y: 0, width: chatViewWidth, height: frame.height)
    chatTextView.frame = CGRect(x: chatView.frame.origin.x + 10, y: 0, width: chatView.frame.width - 20, height: chatView.frame.height)
}

func setupViews() {    

    if isTextFromCurrentUser {
        chatTextView.frame = CGRect(x: 10, y: 0, width: frame.width - 140, height: frame.height)
        chatTextView.backgroundColor = .white
    } else {
        chatTextView.frame = CGRect(x: frame.width - 150, y: 0, width: frame.width - 140, height: frame.height)
        chatTextView.backgroundColor = .blue
    }

    chatTextView.font = UIFont.systemFont(ofSize: 16)
    chatTextView.layer.cornerRadius = 9
    chatTextView.clipsToBounds = true
    chatTextView.autoresizingMask = UIViewAutoresizing.flexibleHeight
    chatTextView.isScrollEnabled = false

    contentView.addSubview(chatView)
    contentView.addSubview(chatTextView)
}

1 个答案:

答案 0 :(得分:1)

化疗,

我相信它是一个聊天泡泡,你正试图设置它的高度和聊天泡沫不能有任何滚动内部确保你的textView的滚动被禁用。

其次,聊天气泡应该根据内容增加其高度,并且没有高度限制使用CGFloat.greatestFiniteMagnitude作为计算boundingRect时可以容纳的高度

func widthHeight(font: UIFont) -> CGRect {
    let constraintRect = CGSize(width: 200, height: CGFloat.greatestFiniteMagnitude)
    let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    return boundingBox
}

最后确保没有{text}的contentInset设置。如果contentInset设置为左5和右5,请确保从最大宽度中减去10(5 + 5)。

由于高度是公式设置宽度中唯一的变量,因此确切地说是获得正确高度的关键。确保将行选项设置为与textViews属性匹配正确。

<强>建议:

UITableView可以使用单元格的自动高度,并在textView上设置滚动禁用使textView根据文本集计算其大小。我的意思是textView将尊重隐式大小。

由于我相信您正在创建一个聊天应用,其中每个气泡都是一个单元格,请考虑使用UITableView的更明智的选择并利用自动单元格高度的好处然后搞乱使用collectionView,它希望您手动提供每个项目的大小

一撮建议:D

我个人使用了边界矩形并设法在加载试验和错误方法后计算文本的确切高度。我个人建议创建一个textView实例,将其属性设置为与故事板中textView的属性完全匹配,然后设置要显示的文本,并使用sizeThatFits获取textView的实际大小,这样更容易。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       let textView = UITextView(frame: CGRect.zero)
       //set textView property here 
       textView.text = self.chatLog[indexPath.row].text
       let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: CGFloat.greatestFiniteMagnitude))
       return size;
}
相关问题