Skip to content

Instantly share code, notes, and snippets.

@yosshi4486
Created August 12, 2021 06:50
Show Gist options
  • Select an option

  • Save yosshi4486/6fdffdbf8adb1df0be33dfc59d13c497 to your computer and use it in GitHub Desktop.

Select an option

Save yosshi4486/6fdffdbf8adb1df0be33dfc59d13c497 to your computer and use it in GitHub Desktop.
Convert a string to UIImage by using UIGraphicsImageRenderer.
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