从UITextView删除/删除NSTextAttachment

我有一个UITextView ,它将混合使用图像(如NSTextAttachment )和字符串。 UITextView不可选,所以我可以使用:

 - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange 

如何删除方法中的textAttachment

您可以使用NSMutableAttributedString replaceCharactersInRange:withString:来删除附件(您将范围作为UITextViewDelegate方法的参数):

 //Retrieve the attributed string NSMutableAttributedString *mutableAttr = [[textView attributedText] mutableCopy]; //Remove the attachment [mutableAttr replaceCharactersInRange:range withString:@""]; //Set the new attributed string [textView setAttributedText:mutableAttr];