Skip to content

Instantly share code, notes, and snippets.

@ochim
Created June 15, 2018 03:42
Show Gist options
  • Select an option

  • Save ochim/87168add265a66ddcaaf13f3ac0ccd76 to your computer and use it in GitHub Desktop.

Select an option

Save ochim/87168add265a66ddcaaf13f3ac0ccd76 to your computer and use it in GitHub Desktop.
[swift4]文字列に合うサイズ領域を求める
// Xcode 9.4
// Swift 4.1
static func calculateRect(text: String) -> CGRect {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
let attributeDict = [
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0),
NSAttributedStringKey.paragraphStyle: paragraphStyle
]
let constraintsSize = CGSize(width: 250, height: 44)
return NSString(string: text).boundingRect(with: constraintsSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributeDict, context: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment