2016-08-07 31 views
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裏面是存在於bubbleView內部約束如下一個textView

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) 
} 

功能estimateframefortext估計的小區的基於文本的畫面(不知何故,不知道它是如何做到的)。它適用於600個字符左右的消息,但是,如果您編寫更多字符,則會爲該單元格增加一個20-30的高度,受到單元格約束的bubbleView也會採用高度,您現在可以看到清晰文字下的空間。我想知道是否有更精確的函數來估計單元格的高度應該基於多少文本。

enter image description here

回答

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返回內容視圖的大小。