如何在文本字段中允许和获取拟我表情贴纸
How to allow and get Memoji Stickers in a text field
我正在尝试在我的应用程序上使用拟我表情贴纸,但 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
在 UITextField 中将 "allowsEditingTextAttributes" 属性 设置为 YES。
所以...如何获得它是在 textfield/view 所在的视图控制器上实现粘贴方法。
然后你就可以从粘贴板中抓取图像了。基本上
Objective-C
UIImage *image = [[UIPasteboard generalPasteboard] image];
或Swift
let image = UIPasteboard.general.image
这会让你找到 UIImage 然后你可以把它包装在
Objective-c
NSAttributedString *memojiStrong = [[NSAttributedString alloc] initWithAttributedString: image];
Swift
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
对于 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/
中找到更多信息
我正在尝试在我的应用程序上使用拟我表情贴纸,但 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
在 UITextField 中将 "allowsEditingTextAttributes" 属性 设置为 YES。
所以...如何获得它是在 textfield/view 所在的视图控制器上实现粘贴方法。
然后你就可以从粘贴板中抓取图像了。基本上
Objective-C
UIImage *image = [[UIPasteboard generalPasteboard] image];
或Swift
let image = UIPasteboard.general.image
这会让你找到 UIImage 然后你可以把它包装在
Objective-c NSAttributedString *memojiStrong = [[NSAttributedString alloc] initWithAttributedString: image];
Swift
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
对于 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/
中找到更多信息