ios - Get the vertical Size of a UILabel which has more than one line? -
i have uilabel more 1 line:
let servicedescription: uilabel = uilabel() servicedescription.text = "some long text..." servicedescription.numberoflines = 0 servicedescription.sizetofit() self.contentview.addsubview(servicedescription)
then add autolayout constraints:
servicedescription.snp_makeconstraints { (make) -> void in make.top.equalto(self.contentview_snp_top).offset(24) make.left.equalto(self.contentview).offset(20) make.right.equalto(self.contentview).offset(-20) }
now want compute size label need on screen did:
let newsize: cgsize = servicedescription.systemlayoutsizefittingsize(uilayoutfittingcompressedsize) println("servicedescription size: \(newsize)")
the result is:
(3408.0, 95.5)
i did preceding commands in viewdidload().
how correct size of label on screen?
you don't need calculate height, label knows height after has laid out,
override func viewdidload() { super.viewdidload() println(label.frame.height) // 21 label.text = "some long text make text go on more 1 line" label.layoutifneeded() println(label.frame.height) // 101.5 }
Comments
Post a Comment