iosswiftios-stickers

How to allow and get Memoji Stickers in a text field


I'm trying to use Memoji Stickers on my app, but neither UITextField or TextField from SwiftUI displays them on the keyboard. How can I make them show up, and how do I retrieve them afterwards?

I tried different text input traits, but had no success. I even tried using the Twitter keyboard option, because the stickers work in the Twitter app, but it was to no avail.

Here's a picture of how it shows up for me in apps that support it, like WhatsApp, Telegram and Twitter


Solution

  • So... how you can get it is to implement the paste method on the view controller where your textfield/view is.

    Then you can grab the image from the pasteboard. Basically

    Objective-C

    UIImage *image = [[UIPasteboard generalPasteboard] image];
    

    or Swift

    let image = UIPasteboard.general.image
    

    Which will let you find the UIImage then you can wrap it in

    Objective-c NSAttributedString *memojiStrong = [[NSAttributedString alloc] initWithAttributedString: image];

    Swift

    let memojiString = NSAttributedString(attachment: image)
    

    This will let you add it as a string to whatever you need to add it to. You can find a little more for the image -> String if you want to save it for an image: https://www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachment

    and the poster for some credit to get me this far: https://www.reddit.com/r/iOSProgramming/comments/cuz3ut/memoji_in_chat/ey4onqg/?context=3