For 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/