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
NSTextViewis on a Sheet, NO CONTENTS seem to be appearing when the mouse pointer is elsewhere other than hovering above theNSTextView - 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!
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: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:My first example notwithstanding, you could place your call to
-appendAttributedString:inside that block as well: