Convert Apple Emoji (String) to UIImage
Updated for Swift 4.1
Add this extension to your project
import UIKitextension String { func image() -> UIImage? { let size = CGSize(width: 40, height: 40) UIGraphicsBeginImageContextWithOptions(size, false, 0) UIColor.white.set() let rect = CGRect(origin: .zero, size: size) UIRectFill(CGRect(origin: .zero, size: size)) (self as AnyObject).draw(in: rect, withAttributes: [.font: UIFont.systemFont(ofSize: 40)]) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image }}
The code above draws the current String
to an Image Context with a white background color and finally transform it into a UIImage
.
Now you can write
Example
Given a list of ranges indicating the unicode values of the emoji symbols
let ranges = [0x1F601...0x1F64F, 0x2702...0x27B0]
you can transform it into a list of images
let images = ranges .flatMap { $0 } .compactMap { Unicode.Scalar($0) } .map(Character.init) .compactMap { String($0).image() }
Result:
I cannot guarantee the list of ranges is complete, you'll need to search for it by yourself 😉
Here's an updated answer with the following changes:
- Centered: Used
draw(at:withAttributes:)
instead ofdraw(in:withAttributes:)
for centering the text within the resulting UIImage - Correct Size: Used
size(withAttributes:)
for having a resulting UIImage of size that correlates to the actual size of the font. - Comments: Added comments for better understanding
Swift 5
import UIKitextension String { func textToImage() -> UIImage? { let nsString = (self as NSString) let font = UIFont.systemFont(ofSize: 1024) // you can change your font size here let stringAttributes = [NSAttributedString.Key.font: font] let imageSize = nsString.size(withAttributes: stringAttributes) UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) // begin image context UIColor.clear.set() // clear background UIRectFill(CGRect(origin: CGPoint(), size: imageSize)) // set rect size nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes) // draw text within rect let image = UIGraphicsGetImageFromCurrentImageContext() // create image from context UIGraphicsEndImageContext() // end image context return image ?? UIImage() }}
Swift 3.2
import UIKitextension String { func textToImage() -> UIImage? { let nsString = (self as NSString) let font = UIFont.systemFont(ofSize: 1024) // you can change your font size here let stringAttributes = [NSFontAttributeName: font] let imageSize = nsString.size(attributes: stringAttributes) UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) // begin image context UIColor.clear.set() // clear background UIRectFill(CGRect(origin: CGPoint(), size: imageSize)) // set rect size nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes) // draw text within rect let image = UIGraphicsGetImageFromCurrentImageContext() // create image from context UIGraphicsEndImageContext() // end image context return image ?? UIImage() }}
Same thing for Swift 4:
extension String { func emojiToImage() -> UIImage? { let size = CGSize(width: 30, height: 35) UIGraphicsBeginImageContextWithOptions(size, false, 0) UIColor.white.set() let rect = CGRect(origin: CGPoint(), size: size) UIRectFill(rect) (self as NSString).draw(in: rect, withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30)]) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image }}