首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextView、NSTextStorage和循环日志?

NSTextView、NSTextStorage和循环日志?
EN

Stack Overflow用户
提问于 2013-01-30 17:54:50
回答 1查看 704关注 0票数 0

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

代码语言:javascript
复制
NSAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: text];
NSTextStorage *textStorage = [SerialOutput textStorage];
[textStorage beginEditing];
[textStorage appendAttributedString:attrString];
[textStorage endEditing];

我想限制文本,例如,1000行,这样就不会折叠应用程序,因为它将无限期运行。

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

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

向你致敬,琼

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-26 15:06:44

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

代码语言:javascript
复制
// 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];
}

正如我所希望的,它的工作方式是循环日志。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14601047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档