【问题标题】:How to clear UITextView... for real如何真正清除 UITextView...
【发布时间】: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


【解决方案1】:

这个可怕的黑客可以解决问题:

self.textView.text = @"Something, doesn't matter what as long as it's not empty";
self.textView.text = @"";

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

【讨论】:

  • 这也是我(不情愿地)这样做的方式。
【解决方案2】:
textView.text=@"";
[textView insertText:@""];

试试这个!

【讨论】:

    【解决方案3】:

    您必须手动将字体、文本颜色等重置为默认值。设置属性文本总是将 UITextView 的“全局”属性设置为属性字符串中第一个字母的属性。

    【讨论】:

    • 会重置自定义属性吗?编辑:不,它没有。我问的是所有属性,而不仅仅是段落、颜色和字体。
    • 只需添加 self.textView.textColor = ,以及为每个可能已被 NSAttributedString 更改的属性添加类似的内容。
    • UITextView 只公开了少数属性。这还不够。
    • 如果有些属性只能用 NSAttributedString 设置,而 UITextView 不能设置(这听起来很奇怪),您可以尝试“hacky”方法 - 当您删除属性字符串时,使用你喜欢的默认属性(文本无关紧要,只是不要让它的长度为0),将其设置为文本视图的属性文本,然后再次将其删除。
    • 找到了解决方案。无论如何,感谢您提供帮助。 +1
    【解决方案4】:

    在我的应用中,我发现 hpique 建议的 hack 性能不佳:

    self.textView.text = @"Something, doesn't matter what as long as it's not empty";
    self.textView.text = @"";
    

    第一行强制 UITextView 重新布局所有内容。如果在重复使用的单元格中使用 UITextView(例如在 UITableView 中),则成本会很高。

    在 iOS 8 及更高版本中,我发现在我的 UITextView 子类中使用以下重置方法可以提高性能:

    - (void)reset
    {    
        self.text = nil;
        self.font = nil;
        self.textColor = nil;
        self.textAlignment = NSTextAlignmentLeft;
    }
    

    【讨论】:

      【解决方案5】:

      有一个属性在这种情况下很有用。我是typingAttributes

      let textView = UITextView()
      let attributes: [NSAttributedString.Key: Any] = [
          .font: UIFont.systemFont(ofSize: 16, weight: .regular),
          .foregroundColor: UIColor.black,
      ]
      textView.typingAttributes = attributes
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-02
        • 1970-01-01
        • 1970-01-01
        • 2015-08-17
        • 1970-01-01
        • 1970-01-01
        • 2011-04-22
        • 2010-10-22
        相关资源
        最近更新 更多