如何在文本字段中允许和获取Memoji Stickers

时间:2019-06-16 02:05:12

标签: ios swift ios-stickers

我正在尝试在我的应用程序上使用Memoji Stickers,但是SwiftUI的UITextField或TextField都不在键盘上显示它们。如何使它们显示出来,以及以后如何检索它们?

我尝试了不同的文本输入特征,但没有成功。我什至尝试使用Twitter键盘选项,因为贴纸可以在Twitter应用程序中使用,但无济于事。

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

3 个答案:

答案 0 :(得分:2)

所以...如何获取它是在文本字段/视图所在的视图控制器上实现粘贴方法。

然后,您可以从粘贴板上抓取图像。基本上

Objective-C

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

或Swift

let image = UIPasteboard.general.image

哪个可以让您找到UIImage,然后可以将其包装在其中

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

迅速

let memojiString = NSAttributedString(attachment: image)

这将使您可以将它作为字符串添加到需要添加的任何内容。 如果您想将其保存为图像,则可以找到更多的图像->字符串:https://www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachment

和值得一提的海报使我走了这么远: https://www.reddit.com/r/iOSProgramming/comments/cuz3ut/memoji_in_chat/ey4onqg/?context=3

答案 1 :(得分:1)

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

答案 2 :(得分:0)

对于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)

您可以在https://kostyakulakov.ru/how-to-get-memoji-from-uitextfield-or-uitextview-swift-4/

中找到更多信息。