Convert Apple Emoji (String) to UIImage

前端 未结 8 959
野的像风
野的像风 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条回答
  •  Happy的楠姐
    2020-12-12 22:26

    This variation is based on @Luca's accepted answer, but allows you to optionally customize the point size of the font, should result in a centered image, and doesn't make the background color white.

    extension String {
        func image(pointSize: CGFloat = UIFont.systemFontSize) -> UIImage? {
            let nsString = self as NSString
            let font = UIFont.systemFont(ofSize: pointSize)
    
            let size = nsString.size(withAttributes: [.font: font])
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            let rect = CGRect(origin: .zero, size: size)
            nsString.draw(in: rect, withAttributes: [.font: font])
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

提交回复
热议问题