Gukki5
Gukki5

Reputation: 517

Delete / Remove NSTextAttachment from UITextView

I have a UITextView that will have a mixture of images (as NSTextAttachment) 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?

Upvotes: 1

Views: 2298

Answers (1)

Larme
Larme

Reputation: 26006

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];

Upvotes: 3

Related Questions