How to get Memoji from UITextField or UITextView Swift 4

Discussion Memoji for Swift 4

Hello, iOS 13 have released and we got access to Animoji. Memoji not available by default in the default UITextField or UITextView.

For start, we will allow use Memoji in our UITextField or UITextView. Animoji use NSTextAttachment for save image of sticker, because Animoji image contain in NSAttributedString.

To get images of Memoji from textfield.attributedText we need for each attachment to get image from it.

Next, we have image of Memoji and we can send it for server

 

Plan of works:

  1. Enable support Memoji for UITextField or UITextView
  2. Get image of Memoji from attributed string in text field
  3. Send it to server

All code have written for Swift 4 or Swift 5

How to enable Memoji support for UITextField or UITextView

We need activate rich mode for UITextField. It’s activate Memoji in keyboard too.


After activation you will see Memoji sticker’s in your keyboard. It’s add bold, italic and other text attributes

Summary

A Boolean value indicating whether the text view allows the user to edit style information.

Declaration

Discussion

When set to true, the text view allows the user to change the basic styling of the currently selected text. The available style options are listed in the edit menu and only apply to the selection.
The default value of this property is false.

Get image of Memoji from UITextField or UITextView

Okay, i have read https://stackoverflow.com/questions/56615542/how-to-allow-and-get-memoji-stickers-in-a-text-field , in this topic use UIPasteboard.general.image, but i have problem with UIPasteboard.general.image, because after get information from UIPasteboard.general.hasImages i try UIPasteboard.general.image, but i have nil and frozen application for it case.

I suggest using an alternative solution: We will get the image of Memoji from the attributed string from UITextField/UITextView.

In the project I’m working on uses RxSwift, So I can observe attributedString changes with one line:

If you don’t use RxSwift, you can use the override:

Okay, to extract the image from the attachments, we will iterate through them all

After we have extracted all the images, we remove it from our text field / text view.

Next I upload the image to the server

This is all that is needed to work with Animoji.

Universale handler (func memojiHandler):

Result:

3 Комментарии “How to get Memoji from UITextField or UITextView Swift 4

Добавить комментарий

Ваш e-mail не будет опубликован.