How to Detect If a Nsattributedstring Contains a Nstextattachment and Remove It

Can NSAttributedString which contains NSTextAttachment be stored(or restored)?

At last, I used a subclass of NSKeyedArchiver named YYTextArchiver to serialize the NSAttributedString directly, and it worked for me.

Delete / Remove NSTextAttachment from UITextView

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

NSTextAttachment image in attributed text with foreground colour

It looks like there is a bug with the NSTextAttachment(image:) constructor (on iOS 13, at the time of this answer), the following image attachment construction works correctly:

let attachment = NSTextAttachment()
attachment.image = image


Related Topics



Leave a reply



Submit