我正在創建一個小聊天消息應用程序。爲此,我使用集合視圖。我發現了很多代碼,並設法將它們結合在一起以使其工作。但是現在我嘗試在自定義單元格中添加一些額外的標籤,並在組聊天中使用用戶名。但似乎我不能在泡泡視圖中添加任何標籤。 timeLabel位於氣泡視圖下方,儘管我以時間表應該位於textView下的氣泡視圖內的方式設置約束。無法在聊天應用程序的集合視圖中調整單元格大小
下面是代碼:
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(bubbleView)
addSubview(textView)
addSubview(profileImageView)
addSubview(timeView)
addSubview(usernameView)
//x,y,w,h
profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
// profileImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 32).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 32).isActive = true
profileImageView.topAnchor.constraint(equalTo: bubbleView.topAnchor).isActive = true
//x,y,w,h
bubbleViewRightAnchor = bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8)
bubbleViewRightAnchor?.isActive = true
bubbleViewLeftAnchor = bubbleView.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8)
// bubbleViewLeftAnchor?.isActive = false
bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 200)
bubbleWidthAnchor?.isActive = true
bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
//ios 9 constraints
//x,y,w,h
// textView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 8).isActive = true
textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor).isActive = true
// textView.widthAnchor.constraint(equalToConstant: 200).isActive = true
textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: timeView.topAnchor, constant: 8).isActive = true
timeView.rightAnchor.constraint(equalTo: textView.rightAnchor, constant: 8).isActive = true
//timeView.topAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 8).isActive = true
timeView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
timeWidthAnchor = timeView.widthAnchor.constraint(equalToConstant: 40)
timeWidthAnchor?.isActive = true
/* usernameView.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
usernameView.bottomAnchor.constraint(equalTo: textView.topAnchor).isActive = true
nameWidthAnchor = usernameView.widthAnchor.constraint(equalToConstant: 100)
nameWidthAnchor?.isActive = true
usernameView.heightAnchor.constraint(equalToConstant: 20)
nameHeightAnchor?.isActive = true */
}
我認爲這個問題是在集合視圖的sizeForItemAt方法。但說實話,我不知道如何改變這種方法,以便它仍然可以像現在這樣使用textview。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var height: CGFloat = 80
let text: String?
//get estimated height somehow????
if messages[indexPath.item].text != nil {
text = messages[indexPath.item].text
height = estimateFrameForText(text!).height + 20
}
let width = UIScreen.main.bounds.width
return CGSize(width: width, height: height)
}
fileprivate func estimateFrameForText(_ text: String) -> CGRect {
let size = CGSize(width: 200, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], context: nil)
}
明白自己粘貼在一起的代碼。 –
這是嘗試更好地理解它。正如我所說,這只是估計的身高功能,這對我來說並不完全清楚。我明白你爲什麼必須這樣做,結果我知道它在做什麼。但是我試圖改變這個函數的輸出時失敗了。 – AlexVilla147