我正在构建/Users/[Username]/Library/Application Support/Sublime Text 3/Packages/HelloWorld/hello_world.py
。我的聊天气泡限制如下:
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
所有约束都可以正常工作,因为您可以看到聊天气泡的高度锚点被限制为"自我高度锚点#34;自我为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 cell
的高度和宽度,如下所示:
textview
函数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)
}
根据文本估计单元格的框架(不知何故,不知道它是如何做到的)。它适用于600字符以下的消息,但是,如果你写了更多字符,它会为单元格增加20-30倍的高度,被约束到单元格的estimateframefortext
也将采用高度,你在现在可以在文本下看到清晰的空间。我想知道是否有更精确的函数来根据文本的大小来估计单元格的高度。
答案 0 :(得分:0)
如果您使用UITextView
,则可以使用此方法:
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
实现了可滚动的行为,当您放置内容[在本例中为text]时,textView符合协议UIScrollView
,为此,您可以使用返回大小的contentSize内容视图。