Convert Apple Emoji (String) to UIImage

前端 未结 8 956
野的像风
野的像风 2020-12-12 21:53

I need all Apple Emojis.
I can get all the emojis and put them into a String by copying them from the site getemoji but in my app i need the emojis in the right

8条回答
  •  抹茶落季
    2020-12-12 22:43

    Swift 4.2

    I really liked @Luca Angeletti solution. I hade the same question as @jonauz about transparent background. So with this small modification you get the same thing but with clear background color.

    I didn't have the rep to answer in a comment.

    import UIKit
    
    extension String {
        func emojiToImage() -> UIImage? {
            let size = CGSize(width: 30, height: 35)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            UIColor.clear.set()
            let rect = CGRect(origin: CGPoint(), size: size)
            UIRectFill(CGRect(origin: CGPoint(), size: size))
            (self as NSString).draw(in: rect, withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 30)])
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

提交回复
热议问题