如何清除UITextView ...真实

hpi*_*que 6 iphone uitextview nsattributedstring ios ios7

我很惭愧地承认我不知道如何清除UITextView.

通过清除,我的意思是将其文本留空并删除其所有属性.我认为将它设置nil为足够,但第一个字符的属性仍保留在那里,静静地等待,直到我再次输入要添加.

例如,以下代码具有UITextView可以在双击(doubleTapAction:)时清除的vanilla .加载后,我添加了一个自定义属性,清除时也应删除该属性UITextView.

@implementation HPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"test"];
    [attributedString addAttributes:@{@"attribute" : @"value"} range:NSMakeRange(0, 1)];
    self.textView.attributedText = attributedString;
}

- (IBAction)doubleTapAction:(id)sender
{
    self.textView.text = nil;
    // Also tried:
    // self.textView.text = @"";
    // self.textView.attributedText = nil;
    // self.textView.attributedText = [[NSAttributedString alloc] initWithString:@""];
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"%@", textView.attributedText);
}

@end
Run Code Online (Sandbox Code Playgroud)

清除UITextView然后键入字母"d"记录以下内容:

d{
    NSFont = "<UICTFont: 0x8a7e4e0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    attribute = value;
}
Run Code Online (Sandbox Code Playgroud)

我的自定义属性仍然存在!

这是一个错误还是预期的行为?

hpi*_*que 7

这个可怕的黑客行诀:

self.textView.text = @"Something, doesn't matter what as long as it's not empty";
self.textView.text = @"";
Run Code Online (Sandbox Code Playgroud)

不过,确定这是一个错误还是预期的行为会很好.我在文档中找不到任何关于它的内容.