I receive as an input a NSAttributedString
that may contain an image attached as NSTextAttachment
. I need to check if actually such image is attached and, in such case, remove it. I've been looking for related posts with no success, how could I do this?
EDIT: I'm trying this:
let mutableAttrStr = NSMutableAttributedString(attributedString: textView.attributedText)
textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, textView.attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in
if (value as? NSTextAttachment) != nil {
mutableAttrStr.replaceCharacters(in: range, with: NSAttributedString(string: ""))
}
}
If the textView.attributedText
contains more than one attachment (I see several \u{ef}
in its string
), I expected the enumeration to match the condition if (value as? NSTextAttachment) != nil
several times but that block of code is only executed once.
How can I remove all attachments?
Swift 4, XCode 9 answer: