Setting attributed title to tab doesn't work in Document Based App

Hi,

I'm working on making a Document based app for MacOS using AppKit and I wanted the different documents to be displayed as tabs of the same window which after a lot of research I managed to do using this single line:

windowController.window?.tabbingMode = .preferred

which I added inside the makeWindowControllers function of my Document. I figured that in order to be able to style my tabs I should use this method:

windowController.window?.tab.attributedTitle = ...

But whatever I added after this = didn't do anything to my tabs. For example I tried this:

windowController.window?.tab.attributedTitle = NSAttributedString(string: "Hello", attributes: [NSAttributedString.Key.backgroundColor: NSColor.red])

Weirdly, using this method:

windowController.window?.tab.title = ...

did work but is far more limited than the other one. Maybe I did something wrong in my implementation of the tabs but it seems to work properly when I run it, only the colors and fonts don't match what I'd like to make which is why I wanted to change them using the attributed title.

Here's my full code for the function:

override func makeWindowControllers() {
    let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
    if let windowController =
        storyboard.instantiateController(
            withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as? NSWindowController {
        addWindowController(windowController)

        windowController.window?.tabbingMode = .preferred
        windowController.window?.tab.attributedTitle = NSAttributedString(string: "hey", attributes: [NSAttributedString.Key.backgroundColor: NSColor.red])

        // Set the view controller's represented object as your document.
        if let contentVC = windowController.contentViewController as? ViewController {
            contentVC.representedObject = content
            contentViewController = contentVC
        }
    }
}

I would really appreciate some help on this issue!

Thanks by advance,

Cyprien

Replies

I second this! This doesn't work in a non-document based app either. It seems like the property has been deprecated but hasn't been marked as such.

In my tests, the attributedTitle property does have an effect, but if you set it too early in the tab's lifecycle, it gets reset. Setting it a bit later, it seems to properly stick.

However, despite what the docs state*, the title's font size and font color are incorrect, even when left unspecified: the font is slightly too large, and the color is the same for both selected and unselected tabs (and isn't correct for either).

* “Attributes that are left unspecified, including the font and foreground color, are automatically filled in using default values appropriate for the window tab”