我很慚愧承認我不知道如何清除UITextView
。如何清除UITextView ...爲真
通過清除,我的意思是留下它的文本空白,並刪除其所有屬性。我認爲將它設置爲nil
就足夠了,但第一個字符的屬性仍然存在,靜靜地等待,直到我再次輸入。
例如,以下代碼具有可在雙擊(doubleTapAction:
)上清除的香草UITextView
。加載時,我添加了一個自定義屬性,該屬性在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
清除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;
}
我的自定義屬性仍然存在!
這是一個錯誤或預期的行爲?
這是我(不情願)也這樣做的方式。 – JMarsh