Como detectar se um NSAttributedString contém um NSTextAttachment e removê-lo?
Eu recebo como entrada umaNSAttributedString
que pode conter uma imagem anexada comoNSTextAttachment
. Preciso verificar se, na verdade, essa imagem está anexada e, nesse caso, removê-la. Estive procurando posts relacionados sem sucesso, como eu poderia fazer isso?
EDITAR: Estou tentando isso:
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: ""))
}
}
Se otextView.attributedText
contém mais de um anexo (vejo várias\u{ef}
na suastring
), Esperava que a enumeração correspondesse à condiçãoif (value as? NSTextAttachment) != nil
várias vezes, mas esse bloco de código é executado apenas uma vez.
Como posso remover todos os anexos?