DarkMode and TextViews - offering users a choice

Folks;


It seems to me that NSTextViews are an ambivelent participant in DarkMode - view Apple's own Mail and TextEdit as examples.

Apple docs even mention that you might want to use a 'normal' textview for presenting long runs of text (e.g. TextEdit does this)


I'm trying to give the user a choice. Let them decide how a mainTextView in my macOS application displays text.

The gist of this can be summed up as:

if (@available(macOS 10.14, *)) {
     if ([[NSAppearance currentAppearance].name isEqualTo:NSAppearanceNameDarkAqua]) {
         if (tag==1){
             //use whiteText on black
             [self.mainTextView setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
         } else if (tag==2) {
             //use blackText on white
             [self.mainTextView setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameAqua]];
         }
    }
}


This does not work.

I've also tried implementing the appearance change all the way up the superview chain. That does not work either.


Anyone have any tips on how to go about implementing this?


Thank you for any input.

Steve

Replies

Have you verified that this code is being triggered and that the expected branch of your "if" is executing? Are you sure that "self.mainTextView" is not nil and is the view you think it is?

yes to all your questions


the result are:

blacktext on a darkbackground when switching to 'NSAppearanceNameAqua'

whitetext on a darkbackground when switching to 'NSAppearanceNameDarkAqua'


By you inquiry, can I assume that this tactic is more or less the correct way to do this?

In other words - it is not crazytown and there is no other more straight-forward mechanism?


What should be the 'draws background' setting for any element in the superview chain?

the mainTextView is nested inside a number of boxes, tabViews, etc...

Well, I'm not sure if it's the right way or not. However, it occurs to me that you probably need to set the appearance of the containing scroll view, not the text view itself. The scroll view is generally responsible for drawing the background.

Hey Ken;


Based on your comments and then my assumption that your questions inferred that I was on the right track I took the nuclear option:

Xcode Clean

Logout and Restart

Re-launch Xcode

Build and Run


Now the code above works as just as expected!

Thank-you for the nudge!

😀

I didn't see this comment when I posted below.

I've gone back and checked and the code as show above works correctly regardless of whether the containing scrollView is set to drawBackground or not.


Changing the appearance of the textView appears (😉) to do the job