Skip to content

Instantly share code, notes, and snippets.

@JayachandraA
Last active May 4, 2017 12:19
Show Gist options
  • Save JayachandraA/8ee5a9d1f1d98c0abdf2a8907cc13836 to your computer and use it in GitHub Desktop.
Save JayachandraA/8ee5a9d1f1d98c0abdf2a8907cc13836 to your computer and use it in GitHub Desktop.
extension UILabel{
func dynamicHeight() -> CGFloat {
let label = UILabel(frame: self.bounds)
label.numberOfLines = self.numberOfLines
label.lineBreakMode = self.lineBreakMode
label.font = self.font
label.text = self.text
label.sizeToFit()
return label.frame.size.height
}
}
let myLable = UILabel(frame: CGRect(x: 10, y: 20, width: 100, height: 20))
myLable.text = "Implements a read-only text view. A label can contain an arbitrary amount of text, but UILabel may shrink, wrap, or truncate the text, depending on the size of the bounding rectangle and properties you set. You can control the font, text color, alignment, highlighting, and shadowing of the text in the label."
myLable.numberOfLines = 0
let expectedHeight = myLable.dynamicHeight()
myLable.frame = CGRect(x: 10, y: 20, width: 100, height: expectedHeight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment