Search code examples
objective-cmacosnsscrollviewnscolor

Objective-C NSScrollView Foreground Colour


I am getting into app development and just started using NSScrollViews to display large chunks of text.

I am able to set the background colour by writing the following:

[_HeadersScrollView setBackgroundColor:[NSColor darkGrayColor]];

But not set the foreground colour by doing something similar to:

[_HeadersScrollView setTextColor:[NSColor whiteColor]]; // nope
[_HeadersScrollView setForegroundColor:[NSColor whiteColor]]; // nope
[_HeadersScrollView setForeground:[NSColor whiteColor]]; // nope

Is there a method or any other way that I could get this type of setup to work? I'd really appreciate it.


Solution

  • The NSScrollView contains an NSClipView (a helper for scrolling) which contains the NSTextView. If you want to operate on the text view you either need an outlet to that or you can request the documentView from the scroll view.

    You can do either:

    [_HeadersScrollView.documentView setTextColor:[NSColor whiteColor]];
    

    Or, if you have an outlet to the text view (called _textView in my example), you can do:

     _textView.textColor = [NSColor whiteColor];
    

    In this case, you would probably want to set the background color on the text view, too, rather than the scroll view. And tell it to draw its background by setting drawsBackground.