ios - swift CollectionView cell textLabel auto resize -
i have watch lot of yt tutorials uicollectionview , uitableview, app searching made watching , reading several tutorials, there 1 problem can not find.
this app made user decide username , wrote text username. have made firebase, connect everything, post getting app, problem is showing 2 lines of text.
this how look, can imagine better:
i textlabel above username expands towards down full text written inside. number of lines on text label set 0. have tried several things in storyboard, lot of different codes , approaches, result same. should in tableview? thought collectionview better approach , easier design-wise.
here codes page 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] { (_,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) }*/ }
you can see codes have testing in "comment" way, because did nothing different.
can please tell me doing wrong? in storyboard, or did insert wrong code, maybe did not inserted needed code @ all?
you can update label's preferredwidth
or alternatively add height constraint greater or equal value.
p.s. make sure have numberoflines
property set 0 label doesn't have max number of lines truncate string well.
Comments
Post a Comment