简体   繁体   English

如何在文本字段中允许和获取拟我表情贴纸

[英]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.我正在尝试在我的应用程序上使用拟我表情贴纸,但 SwiftUI 的 UITextField 或 TextField 都没有在键盘上显示它们。 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.我什至尝试使用 Twitter 键盘选项,因为贴纸在 Twitter 应用程序中有效,但无济于事。

Here's a picture of how it shows up for me in apps that support it, like WhatsApp, Telegram and Twitter这是它如何在支持它的应用程序中显示给我的图片,例如 WhatsApp、Telegram 和 Twitter

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目标-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这会让你找到 UIImage 然后你可以把它包装起来

Objective-c NSAttributedString *memojiStrong = [[NSAttributedString alloc] initWithAttributedString: image]; 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如果您想将其保存为图像,您可以为图像找到更多内容 -> 字符串: https : //www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-带有 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以及让我走到这一步的功劳海报: https : //www.reddit.com/r/iOSProgramming/comments/cuz3ut/memoji_in_chat/ey4onqg/?context=3

在 UITextField 中将“allowsEditingTextAttributes”属性设置为 YES。

For RxSwift:对于 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/您可以在https://kostyakulakov.ru/how-to-get-memoji-from-uitextfield-or-uitextview-swift-4/ 中找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM