skip to Main Content

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

3

Answers


  1. set the “allowsEditingTextAttributes” property to YES in UITextField.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search