Search code examples
swiftmacosnsattributedstringappkitnsmenuitem

NSMenuItem with attributedTitle containing an NSFont object draws the title with baseline shift


I'm tying to create an NSPopUpButton with the list of fonts available in the system. Seemed pretty obvious task but I've failed. I guess, I'm missing something so obvious that I've completely forgot about it.

The code is pretty straight:

let button = NSPopUpButton()
button.menu = NSMenu()

NSFontManager.shared.availableFonts.forEach { fontNameString in
            let item = NSMenuItem()
            let font = NSFont(name: fontNameString, size: 14)!

            let attrs: [NSAttributedString.Key: Any] = [.font: font]
            item.attributedTitle = NSAttributedString(string: fontNameString, attributes: attrs)

            button.menu?.addItem(item)
}

But that just creates the NSMenu with items having shifted baselines. I've tried to calculate the baseline offset and add it as an attribute but I've failed. Haven't found an algorhythm to satisfy all the font available in the system.

Besides, adding the baseline offset resizes the corresponding NSMenuItem which does not have a fixed size, by the way - the height of an item is different on every font.

To analyse the situation I've added the .backgroundColor attribute and set it to red NSColor. And that brought even more confusing. It appears that some font somehow not drawing in bounds.

How can I center the attributed title vertically? Please, help!

That's how it looks


Solution

  • Probably, that is NSAttributedString's issue.

    To workaround, I created a custom view and drew a string in it with a trick. Then set it to NSMenuItem.view.

    enter image description here

    Get more details, see my codes below. https://github.com/bluedome/FontSelectionView/blob/main/FontSelectionView.swift

    Hope it help, if you're still having the trouble...