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)}
当这段代码运行时,我会大量计算错误的单元格大小:
正如您所看到的,视图的帧非常混乱.
第一行是“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,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无法正常工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)