![](/img/trans.png)
[英]How to change textLabel outside CollectionView when cell is clicked in Swift 4?
[英]swift CollectionView cell textLabel auto resize
我看過很多關於UICollectionView和UITableView的YT教程,即使我正在尋找幫助的該應用程序也是通過觀看和閱讀幾個教程而制成的,但是我找不到一個問題。
該應用由用戶決定其用戶名,並使用該用戶名編寫了一些文本。 我已經使用Firebase做到了這一點,連接了所有東西,甚至發布都回到了應用程序中,但是問題是總是只顯示兩行文本。
這是它的外觀,因此您可以想象得更好:
我希望用戶名上方的textLabel向下擴展,里面寫有全文。 該文本Label上的行數設置為0。我在情節提要中嘗試了幾件事,使用了許多不同的代碼和方法,但結果始終相同。 我應該在TableView中執行此操作嗎? 我認為collectionView是更好的方法,更易於進行設計。
這是此頁面VC的代碼:
import UIKit
import Firebase
class FeedViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
var fetchPosts = [Post]()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func loadData() {
self.fetchPosts.removeAll()
let ref = Database.database().reference()
ref.child("Frusters").observeSingleEvent(of: .value, with: { (snapshot) in
if let postDict = snapshot.value as? [String:AnyObject] {
for (_,postElement) in postDict {
print(postElement);
let post = Post()
post.username = postElement["Username"] as? String
post.message = postElement["Message"] as? String
self.fetchPosts.append(post)
}
}
self.collectionView.reloadData()
}) { (error) in
print(error.localizedDescription)
}
ref.removeAllObservers()
}
/*func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let deviceSize = UIScreen.main.bounds.size
//let cellSize = sqrt(Double(deviceSize.width * deviceSize.height) / (Double(33)))
let cellWidth = ((deviceSize.width / 2) - 10)
let cellHeight = (deviceSize.height / 4)
return CGSize(width: cellWidth , height: cellHeight)
}*/
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.fetchPosts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "messageCell", for: indexPath) as! PostCell
cell.messageLabel.text = self.fetchPosts[indexPath.row].message
cell.usernameLabel.text = self.fetchPosts[indexPath.row].username
return cell
}
/*func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeFotItemAt indexPath: IndexPath) -> CGSize {
return CGSize (width: view.frame.width, height: 500)
}*/
}
您可以看到我已經對其進行測試的某些代碼現在處於“注釋”方式,因為它們沒有什么不同。
你能告訴我我做錯了嗎? 這是情節提要中的問題,還是我插入了錯誤的代碼,也許根本沒有插入任何需要的代碼?
您可以更新標簽的preferredWidth
或者僅添加一個大於或等於value的高度約束。
PS確保將numberOfLines
屬性設置為0,以便標簽沒有將截斷字符串的最大行數。
您可以基於估計字符串將采用的高度,為每個單元格分配動態高度。 然后,如果您的標簽的numberOfLines
設置為0
(無窮大),或者說是numberOfLines
,那么它將按您希望的那樣工作。
注意:每個單元格內標簽的高度限制也應設置為可以擴展到要顯示的多行的方式。
估計字符串的高度:
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(boundingBox.height)
}
}
UICollectionView布局委托
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeFotItemAt indexPath: IndexPath) -> CGSize {
let messageHeight = self.fetchPosts[indexPath.row].message.height(yourConstrainedWidth, yourFont)
let height = height of other stuff including margins + messageHeight
return CGSize (width: view.frame.width, height: height )
}
謝謝你們。 你說的沒錯。 我已經在TableView中做到了,而且結果相同也容易得多。 我已經做到了,因此我將移至此應用程序的下一個層次。
謝謝大家的幫助;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.