Appending to NSTextView

旧城冷巷雨未停 提交于 2019-12-22 20:33:00

问题


I've got an NSTask (with an NSPipe set up) running in the background and I want to output the contents, as they're coming in, in an NSTextView (output).

The code I'm using is :

NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithString:s];
//[str addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0, [str length])];

[[output textStorage] appendAttributedString:str];

[output scrollRangeToVisible:NSMakeRange([[output string] length], 0)];

Issues :

  • When there is a lot of data appending, the view seems like "flashing"... and not working properly.
  • Given that the NSTextView is on a Sheet, NO CONTENTS seem to be appearing when the mouse pointer is elsewhere other than hovering above the NSTextView
  • Why is that, although I've set the color/insertion color/etc of the NSTextView, this doesn't seem to apply to newly inserted text?
  • What's the suggested way of appending (+scrolling) on an NSTextView?

Thanks!


回答1:


Remember that user interface elements, and this includes NSTextView, do their magic on the main thread. If you're attempting to add information to the text view, that's where you'd best be doing it. Here's how:

[[output textStorage] performSelectorOnMainThread:@selector(appendAttributedString:) 
                                       withObject:str 
                                    waitUntilDone:YES];

I'd address your third point, but frankly, that's a thing of which I'm still very much a student.

To address your fourth point, it would appear you've got that figured out; just combine your append and scroll actions. But just like changing the contents of textStorage, you want to be sure you're doing this on the main thread. Since -scrollRangeToVisible: doesn't take an object for its argument, you have to do this a bit differently:

dispatch_async(dispatch_get_main_queue(), ^{
    [output scrollRangeToVisible:NSMakeRange([[output string] length], 0)];
});

My first example notwithstanding, you could place your call to -appendAttributedString: inside that block as well:

dispatch_async(dispatch_get_main_queue(), ^{
    [[output textStorage] appendAttributedString:str];
    [output scrollRangeToVisible:NSMakeRange([[output string] length], 0)];
});



回答2:


Regarding the recommended way of appending to the NSTextView: You're doing quite well with appendAttributedString:, but it's recommended to shield it inside shouldChangeTextInRange, then a beginEditing, appendAttributedString, and finally endEditing:

textStorage = [textView textStorage];
if([textView shouldChangeTextInRange:range replacementString:string])
{
    [textStorage beginEditing];
    [textStorage replaceCharactersInRange:range withAttributedString:attrStr];
    // or if you've already set up the attributes (see below)...
    // [textStorage replaceCharactersInRange:range withString:str];
    [textStorage endEditing];
}

I'd strongly suggest replacing scrollRangeToVisible: by scrollToPoint:, as scrollRangeToVisible: will cause a lot of flickering and it will also gradually become slower as you move 'down the range'.

A quick-and-dirty way could be something like this:

- (void)scrollToBottom
{
    NSPoint     pt;
    id          scrollView;
    id          clipView;

    pt.x = 0;
    pt.y = 100000000000.0;

    scrollView = [self enclosingScrollView];
    clipView = [scrollView contentView];

    pt = [clipView constrainScrollPoint:pt];
    [clipView scrollToPoint:pt];
    [scrollView reflectScrolledClipView:clipView];
}

I let constrainScrollPoint do all the calculation work. I do this, because my calculations failed anyway (those suggested by Apple and others, that used visRect/docRect coordinates, produced unreliable results). reflectScrolledClipView is also important; it updates the scroll bar so it has the correct proportion and position.

You might also find it interesting to know when scrolling has occurred. If so, subscribe to both NSViewBoundsDidChangeNotification and NSViewFrameDidChangeNotification. When one of them occurs, the scroll bar position most likely changed (investigate [textView visibleRect] and [textView bounds]).

I see you also have trouble with the text-attributes. So did I for a long time. I found that appending an attributed string would help quite a lot, but it still wasn't enough for the text being typed. ..Then I found out about typingAttributes. When setting up your NSTextView, for instance in an -awakeFromNib, you can pick what you like from the following...

NSMutableParagraphStyle *paragraphStyle;
float                   characterWidth;
NSFont                  *font;
uint32_t                tabWidth;
NSMutableDictionary     *typingAttributes;

tabWidth = 4;
font = [NSFont fontWithName:@"Monaco" size:9.0];
paragraphStyle = [[textView defaultParagraphStyle] mutableCopy];
if(NULL == paragraphStyle)
{
    paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    // or maybe:
    // paragraphStyle = [NSParagraphStyle new];
}
characterWidth = [[font screenFontWithRenderingMode:NSFontDefaultRenderingMode] advancementForGlyph:(NSGlyph)' '].width;
[paragraphStyle setDefaultTabInterval:(characterWidth * (float) tabWidth];
[paragraphStyle setTabStops:[NSArray array]];
typingAttributes = [[textView typingAttributes] mutableCopy];
if(NULL == typingAttributes)
{
    typingAttributes = [NSMutableDictionary new];
}
[typingAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[typingAttributes setObject:font forKey:NSFontAttributeName];
[textView setTypingAttributes:attributes];

...It's way more than you probably need, but it shows how you can set the font, the tab width and the typing attributes. NSForegroundColorAttributeName might also be interesting for you (as well as some other attributes, type NSForegroundColorAttributeName in Xcode and option-double-click on it, then you'll see some more attributes (you can command-double-click as well; this takes you to the definition in the header file).



来源:https://stackoverflow.com/questions/9993008/appending-to-nstextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!