2016-06-21 177 views
-1

我有一個UITextView,它會混合使用圖像(如NSTextAttachment)和字符串。該UITextView是不可選的,所以我可以使用:從UITextView中刪除/刪除NSTextAttachment

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

如何刪除方法textAttachment

+0

'NSMutableAttributedString * mutableAttr = [[TextView的attributedText] mutableCopy]; [mutableAttr replaceCharactersInRange:range withString:@「」]; [textView setAttributedText:mutableAttr];'? – Larme

+0

真棒!發佈這個解決方案,我會接受它 – Gukki5

回答

1

可以使用的NSMutableAttributedStringreplaceCharactersInRange:withString:以除去attachement(你得到的範圍作爲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];