【发布时间】:2014-03-10 23:19:24
【问题描述】:
我很惭愧地承认我不知道如何清除UITextView。
通过清除,我的意思是将其文本留空并删除其所有属性。我认为将其设置为nil 就足够了,但是第一个字符的属性仍然存在,默默地等待直到我再次输入才能添加。
例如,以下代码有一个普通的UITextView,可以通过双击清除 (doubleTapAction:)。加载时,我添加了一个自定义属性,当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;
}
我的自定义属性还在!
这是错误还是预期行为?
【问题讨论】:
-
+1 用于 UITextView 中的另一个奇怪问题。在过去的几天里,我一直卡在 UITextView 的使用上,发现了 3-4 个主要问题,比如额外的填充、大小和这个。
标签: ios iphone ios7 uitextview nsattributedstring