2

我正在尝试在我的应用程序上使用拟我表情贴纸,但 SwiftUI 的 UITextField 或 TextField 都不会在键盘上显示它们。我怎样才能让它们出现,之后我该如何取回它们?

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

这是它在支持它的应用程序(如 WhatsApp、Telegram 和 Twitter)中如何显示给我的图片

4

3 回答 3

3

所以......你怎么能得到它是在你的文本字段/视图所在的视图控制器上实现粘贴方法。

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

Objective-C

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

或斯威夫特

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-属性字符串与 nstextattachment

和一些信用让我走到这一步的海报: https ://www.reddit.com/r/iOSProgramming/comments/cuz3ut/memoji_in_chat/ey4onqg/?context=3

于 2019-08-26T07:10:33.623 回答
1

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

于 2019-08-15T13:56:36.340 回答
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/中找到更多信息

于 2019-10-04T13:39:16.440 回答