Matheus Felizola
Matheus Felizola

Reputation: 33

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

Upvotes: 3

Views: 2721

Answers (3)

KonstantinKulakov
KonstantinKulakov

Reputation: 51

For RxSwift:

 textView.rx.attributedText.subscribe(onNext: { [weak self] attributeString in
        guard let attributeString = attributeString else { return }

        attributeString.enumerateAttribute(NSAttributedStringKey.attachment, in: NSRange(location: 0, length: attributeString.length), options: [], using: {(value,range,_) -> Void in
            if (value is NSTextAttachment) {
                let attachment: NSTextAttachment? = (value as? NSTextAttachment)
                var image: UIImage?

                if ((attachment?.image) != nil) {
                    image = attachment?.image
                } else {
                    image = attachment?.image(forBounds: (attachment?.bounds)!, textContainer: nil, characterIndex: range.location)
                }

                guard let pasteImage = image else { return }

                guard let pngData = UIImagePNGRepresentation(pasteImage) else { return }
                guard let pngImage = UIImage(data: pngData) else { return }

                guard let attachmentImage = OutgoingFile(image: pngImage, type: .image, isPNG: true) else { return }

                let newString = NSMutableAttributedString(attributedString: attributeString)
                newString.replaceCharacters(in: range, with: "")

                self?.newCommentBodyTextView.textView.attributedText = newString

                self?.addFileOnNews(attachmentImage)

                return
            }
        })
    }).disposed(by: bag)

You can find more information in https://kostyakulakov.ru/how-to-get-memoji-from-uitextfield-or-uitextview-swift-4/

Upvotes: 0

Daemedeor
Daemedeor

Reputation: 1007

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

Upvotes: 3

Anson Lau
Anson Lau

Reputation: 21

set the "allowsEditingTextAttributes" property to YES in UITextField.

Upvotes: 2

Related Questions