【问题标题】:How to allow and get Memoji Stickers in a text field如何在文本字段中允许和获取拟我表情贴纸
【发布时间】:2019-06-16 02:05:12
【问题描述】:

我正在尝试在我的应用程序上使用拟我表情贴纸,但 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

【问题讨论】:

  • 您需要创建一个“键盘扩展”。也许可以看到stackoverflow.com/a/37019342/95309 - 它们在 iOS 上也被称为“贴纸”,这将帮助您找到更合适的资源。请注意,支持这一点需要做很多工作(自定义键盘、自定义输入字段、自定义渲染)。开箱即用不是您可以轻松支持的东西。

标签: ios swift ios-stickers


【解决方案1】:

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

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

目标-C

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

或斯威夫特

let image = UIPasteboard.general.image

这会让你找到 UIImage 然后你可以把它包装起来

目标-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

【讨论】:

  • 感谢您的回答。我观察到的是,当您选择拟我表情时,它会自动添加到粘贴板。但是当我访问 pasteboard.image 时它返回 nil。否则我将如何将拟我表情粘贴到粘贴板上?
  • @alionthego 您至少需要像我在答案中那样访问通用粘贴板,然后再获取图像。仅仅做图像对我来说也总是为零。我写的东西没有捷径可走。
  • 您能否展示如何使用 swift 从粘贴板 UIPasteboard.general.image 中准确获取 UIImage。因为该文件应该是 UIImage 文件,但它是空的
【解决方案2】:

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

【讨论】:

  • memoji 贴纸现在出现在键盘上,但我怎样才能找回它们?
  • 有人知道如何从粘贴板上获取贴纸吗?
  • 您通过覆盖文本字段的“attributedText”属性的设置器来接收它,然后通过属性文本的 TextAttachment 对象接收它。
【解决方案3】:

对于 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/找到更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多