Created
August 12, 2021 06:50
-
-
Save yosshi4486/6fdffdbf8adb1df0be33dfc59d13c497 to your computer and use it in GitHub Desktop.
Convert a string to UIImage by using UIGraphicsImageRenderer.
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
| let string = "🤩" | |
| let bounds = CGRect(x: 0, y: 0, width: 30, height: 30) | |
| let image = UIGraphicsImageRenderer(size: bounds.size).image { context in | |
| let attributes: [NSAttributedString.Key: Any] = [ | |
| .font: UIFont.preferredFont(forTextStyle: .body), | |
| .foregroundColor: UIColor.label | |
| ] | |
| let attributedString = NSAttributedString(string: string, attributes: attributes) | |
| attributedString.draw(with: bounds, options: .usesLineFragmentOrigin, context: nil) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment