我在应用程序窗口中有一个NSTextView,它显示串行端口传入数据的日志。当日志到达应用程序时,我将文本附加到日志中:
NSAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: text];
NSTextStorage *textStorage = [SerialOutput textStorage];
[textStorage beginEditing];
[textStorage appendAttributedString:attrString];
[textStorage endEditing];我想限制文本,例如,1000行,这样就不会折叠应用程序,因为它将无限期运行。
现在我有了一个临时解决方案,基于每周清除日志的NSTimer,它可以工作,但我更喜欢实现一个聪明的方法,只需限制文本大小并创建循环日志。
有什么想法吗?也许可以使用insertAttributedString方法?
向你致敬,琼
发布于 2013-02-26 15:06:44
最后,我找到了一种方法,当我将文本附加到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];
}正如我所希望的,它的工作方式是循环日志。
https://stackoverflow.com/questions/14601047
复制相似问题