【问题标题】:NSTextView, NSTextStorage and circular log?NSTextView、NSTextStorage 和循环日志?
【发布时间】:2013-01-30 09:54:50
【问题描述】:

我在应用程序窗口中有一个 NSTextView,它显示了串行端口的传入数据日志。当它到达应用程序时,我将文本附加到日志中:

NSAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: text];
NSTextStorage *textStorage = [SerialOutput textStorage];
[textStorage beginEditing];
[textStorage appendAttributedString:attrString];
[textStorage endEditing];

我想将文本限制为 1000 行,以免应用程序因无限期运行而崩溃。

现在我有一个临时解决方案,基于每周清除日志的 NSTimer,它可以工作,但我更喜欢实施一个聪明的方法,只是限制文本大小并创建循环日志。

有什么想法吗?也许使用方法 insertAttributedString ?

问候, 琼

【问题讨论】:

    标签: logging size fixed nstextview nstextstorage


    【解决方案1】:

    最后我找到了方法,当我将文本附加到 NSTextStorage 时,我只控制长度是否超过阈值并清理日志开头的一些空间:

    // updates the textarea for incoming text by appending text
    - (void)appendToIncomingText: (id) text {
      // add the text to the textarea
      NSAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: text];
      NSTextStorage *textStorage = [SerialOutput textStorage];
      [textStorage beginEditing];
      [textStorage appendAttributedString:attrString];
          //Max. size of TextArea: LOG_SIZE characters
          if ([textStorage length] > LOG_SIZE){
              [textStorage deleteCharactersInRange:NSMakeRange(0, [attrString length])];
          }
      [textStorage endEditing];
    
      // scroll to the bottom
      NSRange myRange;
      myRange.length = 1;
      myRange.location = [textStorage length];
      NS[SerialOutput scrollRangeToVisible:myRange];
    }
    

    如我所愿,它可以用作循环日志。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多