Gukki5 Gukki5 - 1 year ago 266
iOS Question

Delete / Remove NSTextAttachment from UITextView

I have a UITextView that will have a mixture of images (as NSTextAttachments) and character strings. The UITextView is NOT selectable, so I can use:

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

How do I delete the textAttachment in the method?

Answer Source

You can use replaceCharactersInRange:withString: of NSMutableAttributedString to remove the attachement (you got the range as parameter of the UITextViewDelegate method):

//Retrieve the attributed string
NSMutableAttributedString *mutableAttr = [[textView attributedText] mutableCopy];
//Remove the attachment
[mutableAttr replaceCharactersInRange:range withString:@""]; 
//Set the new attributed string
[textView setAttributedText:mutableAttr];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download