0

我正在构建一个Chatting View Controller. 我的聊天气泡的约束如下:

bubbleViewRightAnchor = bubbleView.rightAnchor.constraintEqualToAnchor(self.rightAnchor, constant: -8)
bubbleView.topAnchor.constraintEqualToAnchor(self.topAnchor).active = true
bubbleWidthAnchor = bubbleView.widthAnchor.constraintEqualToConstant(200)
bubbleWidthAnchor?.active = true
bubbleViewRightAnchor?.active = true
bubbleView.heightAnchor.constraintEqualToAnchor(self.heightAnchor).active = true

在里面bubbleView是一个textView,它被限制在里面bubbleView,如下所示:

textView.leftAnchor.constraintEqualToAnchor(bubbleView.leftAnchor, constant: 8).active = true
textView.topAnchor.constraintEqualToAnchor(self.topAnchor).active = true
textView.rightAnchor.constraintEqualToAnchor(bubbleView.rightAnchor).active = true
textView.heightAnchor.constraintEqualToAnchor(self.heightAnchor).active = true

所有的约束都很好,你可以看到聊天气泡的高度锚被限制为“自我的高度锚”,自我是collection View cell. 因此,无论细胞有什么高度,气泡也会有。气泡内部是一个textView包含用户发送的所有文本的。在另一个中,以下代码根据文本的数量View Controller修改高度和宽度,如下所示:collection view celltextview

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    var height: CGFloat = 80

    if let text = messages[indexPath.item].text {
        height = estimateFrameForText(text).height + 20
    }
    let width = UIScreen.mainScreen().bounds.width

    return CGSize(width: width, height: height)
}

private func estimateFrameForText(text: String) -> CGRect {
    let size = CGSize(width: 200, height: 1000)
    let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
    return NSString(string: text).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16)], context: nil)
}

该函数estimateframefortext根据文本估计单元格的框架(不知何故,不知道它是如何做到的)。它适用于 600 个字符左右以下的消息,但是,如果您编写更多字符,它会为单元格增加 20-30 的额外高度,bubbleView被限制到单元格的内容也将采用高度,您现在可以看到清晰的空间文下。我想知道是否有更精确的函数来估计单元格的高度应该基于有多少文本。

在此处输入图像描述

4

1 回答 1

0

如果您使用的是UITextViewthen,则可以使用此方法:

import UIKit
import XCPlayground

let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 500))
view.backgroundColor = UIColor.whiteColor()
XCPlaygroundPage.currentPage.liveView = view

let text = "The function estimateframefortext estimates the frame of the cell based on the text (somehow, no idea how it does it). It works fine for messages under 600 characters or so, however, if you write more characters it adds an extra 20-30 of height to the cell, the bubbleView being constrained to the cell will also adopt the height, where you can now see clear space under the text. I was wondering if there were more precise functions to estimate what the height of a cell should be. \n!!!!!The function estimateframefortext estimates the frame oheightTextThe function estimateframefortext estimates the frame of the cell based on the text (somehow, no idea how it does it). It works fine for messages under 600 characters or so, however, if you write more characters it adds an extra 20-30 of height to the cell, the bubbleView being constrained to the cell will also adopt the height, where you can now see clear space under the text. I was wondering if there were more precise functions to estimate what the height of a cell should be "

let label = UITextView()
view.addSubview(label)
label.text = text

let size = label.sizeThatFits(CGSize(width: view.bounds.width, height: 0))
let heightText = size.height
debugPrint(label.contentSize)
label.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: heightText)
label.layer.borderWidth = 1
label.layer.borderColor = UIColor.redColor().CGColor

或者:

let label = UITextView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 0))
view.addSubview(label)
label.text = text
debugPrint(label.contentSize)
label.frame.size = label.contentSize
label.layer.borderWidth = 1
label.layer.borderColor = UIColor.redColor().CGColor

解释:

UITextView实现可滚动的行为,当您放置内容[在本例中为文本]时,textView 符合协议UIScrollView,为此,您可以使用 contentSize 返回内容视图的大小。

于 2016-08-07T23:55:49.157 回答