Skip to content

Instantly share code, notes, and snippets.

@bgayman
Created March 27, 2016 14:47
Show Gist options
  • Save bgayman/da9ae1f752e9062fa099 to your computer and use it in GitHub Desktop.
Save bgayman/da9ae1f752e9062fa099 to your computer and use it in GitHub Desktop.
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 300.0, height: 100.0))
label.numberOfLines = 0
label.backgroundColor = UIColor.yellowColor()
label.lineBreakMode = .ByWordWrapping
label.text = "Hello 😇\n🤑蠓蝾"
let fontSize:CGFloat = 54.0
label.font = UIFont.systemFontOfSize(fontSize)
let size = label.sizeThatFits(CGSize(width: 100.0, height: CGFloat.max))
label
label.sizeToFit()
print(label.frame)
var style:NSMutableParagraphStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
style.lineBreakMode = .ByWordWrapping
let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(fontSize),NSParagraphStyleAttributeName:style]
let tex:NSString = label.text!
let rect = tex.boundingRectWithSize(CGSize(width: 100.0, height: CGFloat.max), options: [NSStringDrawingOptions.UsesLineFragmentOrigin,NSStringDrawingOptions.UsesFontLeading], attributes: attributes, context: nil)
label.frame = rect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment