【问题标题】:Delete / Remove NSTextAttachment from UITextView从 UITextView 中删除/移除 NSTextAttachment
【发布时间】:2016-06-21 18:20:18
【问题描述】:

我有一个UITextView,它将混合图像(如NSTextAttachment)和字符串。 UITextView 是不可选择的,所以我可以使用:

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

如何删除方法中的textAttachment

【问题讨论】:

  • NSMutableAttributedString *mutableAttr = [[textView attributedText] mutableCopy]; [mutableAttr replaceCharactersInRange:range withString:@""]; [textView setAttributedText:mutableAttr];?
  • 太棒了!将此作为解决方案发布,我会接受它

标签: ios uitextview nstextattachment


【解决方案1】:

您可以使用NSMutableAttributedStringreplaceCharactersInRange:withString: 删除附件(您将范围作为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];

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 1970-01-01
    • 2012-09-06
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多