iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITextView的boundingRect无法正常工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前在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)
}

当这段代码运行时,我会大量计算错误的单元格大小:

ios – UITextView的boundingRect无法正常工作

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

这是我的@R_301_2483@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,width: chatView.frame.width - 20,height: chatView.frame.height)
}

func setupViews() {    

    if isTextFromCurrentUser {
        chatTextView.frame = CGRect(x: 10,width: frame.width - 140,height: frame.height)
        chatTextView.BACkgroundColor = .white
    } else {
        chatTextView.frame = CGRect(x: frame.width - 150,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)
}

解决方法

化疗,

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

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

func widthHeight(font: UIFont) -> CGRect {
    let consTraintRect = CGSize(width: 200,height: CGFloat.greatestFiniteMagnitudE)
    let boundingBox = self.boundingRect(with: consTraintRect,context: nil)
    return boundingBox
}

最后确保没有为textView设置contenTinset.如果contenTinset设置为left 5和right 5,请确保从最大宽度中减去10(5 5).

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

建议:

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

我相信你正在创建一个聊天应用程序,其中每个气泡都是一个单元格,虑使用UITableView的更合理的选择,并利用自动单元格高度的好处,然后搞乱collectionView,希望你手动提供每个项目的大小.

捏建议:D

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

func collectionView(_ collectionView: UICollectionView,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;
}

大佬总结

以上是大佬教程为你收集整理的ios – UITextView的boundingRect无法正常工作全部内容,希望文章能够帮你解决ios – UITextView的boundingRect无法正常工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。