从 UITextView 中删除/删除 NSTextAttachment

Delete / Remove NSTextAttachment from UITextView

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

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

如何删除方法中的textAttachment

您可以使用 replaceCharactersInRange:withString:NSMutableAttributedString 删除附件(您已将范围作为 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];