Search code examples
swiftcocoansbutton

How to make NSButton title at bottom and centered?


How can I make a button's title at bottom and centered programmatically?


Solution

  • Subclass NSButtonCell and override drawTitle:withFrame:inView:(drawTitle(_,withFrame,inView) in swift) to draw the title centered by manipulating frame parameter for example, or draw the title by yourself. Then set your button's cell class to your subclass.

    Or place NSTextField over your button, set constraints to anchor the bottomcenter of the button and set its text to whatever you want, and keep your buttons title empty.

    Unfortunately, there is no contentHorizontalAlignment property, like in iOS, so you have to solve it with subclass or a different UI composing.