Created
June 15, 2018 03:42
-
-
Save ochim/87168add265a66ddcaaf13f3ac0ccd76 to your computer and use it in GitHub Desktop.
[swift4]文字列に合うサイズ領域を求める
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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