Convert Apple Emoji (String) to UIImage

前端 未结 8 933
野的像风
野的像风 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:45

    Updated @Luca Angeletti answer for Swift 3.0.1

    extension String {
    
        func image() -> UIImage? {
            let size = CGSize(width: 30, height: 35)
            UIGraphicsBeginImageContextWithOptions(size, false, 0);
            UIColor.white.set()
            let rect = CGRect(origin: CGPoint(), size: size)
            UIRectFill(CGRect(origin: CGPoint(), size: size))
            (self as NSString).draw(in: rect, withAttributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 30)])
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 22:49

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题