How to display two-line Suggestion menu with NSTextField

1 day ago 3
ARTICLE AD BOX

The hack is to use initialiser with AttributedString public init(representedValue: SuggestionItemType, attributedTitle: AttributedString) and "force" font using setAttributes

Another important note: If your minimum deployment target is macOS10.13 with @available(macOS 15, *) macro -> you will need to apply AttributedString even to the subtitle. With macOS10.15 as a minimum target you don't need to set attributes to subtitle.

/// Workaround for a macOS 15 AppKit regression. /// /// Suggestion cells only lay out correctly when using AttributedString with an /// explicit font attribute. Plain strings, or attributed strings without fonts, /// cause NSTableCellView to miscalculate row height, clipping the subtitle. func suggestionAttributedString(_ string: String, fontSize: CGFloat) -> AttributedString { var attributed = AttributedString(string) attributed.setAttributes( AttributeContainer([ .font: NSFont.systemFont(ofSize: fontSize) ]) ) return attributed } func locationItem(_ completion: MKLocalSearchCompletion) -> Item { var item = NSSuggestionItem( representedValue: completion, attributedTitle: suggestionAttributedString( completion.title, fontSize: NSFont.systemFontSize ) ) if !completion.subtitle.isEmpty { item.attributedSecondaryTitle = suggestionAttributedString( completion.subtitle, fontSize: NSFont.smallSystemFontSize ) } return item }

Marek H's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article