Delete / Remove NSTextAttachment from UITextView

老子叫甜甜 提交于 2019-12-02 20:04:43

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/37951812/delete-remove-nstextattachment-from-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!