How to make NSButton title at bottom and centered?
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.
Drawing text in a NSButton subclass - positioning
You want to get the size of the title first using NSString -sizeWithAttributes:
. Then you can use that size to adjust the drawing of title within your bounds.
There is a trick to this, since you will almost always wind up dividing by two at some point in this process. When you do that, you may wind up with a half-pixel result, which Cocoa will happily use. However, it will cause your text to be blurry. You almost always want to call floor()
on your coordinates before drawing on them to get yourself back onto integral pixels.
Edit: A basic example of centering (this should compile correctly, I haven't compiled it and my recent work has been over on iPhone which is upside-down):
NSRect rect;
rect.size = [[self title] sizeWithAttributes:att];
rect.origin.x = floor( NSMidX([self bounds]) - rect.size.width / 2 );
rect.origin.y = floor( NSMidY([self bounds]) - rect.size.height / 2 );
[[self title] drawInRect:rect withAttributes:att];
Anyway, something along those lines. You may want to offset a bit because of how your button draws, but this is the basic idea.
NSButton Title not updating when bound value changes
For bindings to work, the property has to be changed in a KVO way. I'm new to Swift but I found this: Is key-value observation (KVO) available in Swift?. In short: add the dynamic modifier
to the property.
NSButton wrap title dynamically
The credits for this answer goes to @Willeke's comment: "AppKit doesn't support multiline checkboxes." The interesting behaviour of the checkbox in case 1 suggesting otherwise seems to be just a glitch or bug.
What I ended up with: I opted for a workaround. I'm placing a checkbox-button (with Image Position "Image only" in IB) right beside a multiline-label, putting both in a custom view, adding the necessary constraints. With a few positioning adjustments I know have a solution that looks exactly like a singleline checkbox, that I can copy-paste in IB and that is solved by Autolayout - in IB and at runtime - without any additional code.
How can I set the title of a UIButton as left-aligned?
Set the contentHorizontalAlignment:
// Swift
emailBtn.contentHorizontalAlignment = .left;
// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
You might also want to adjust the content left inset otherwise the text will touch the left border:
// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);
// Objective-C
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
Related Topics
Reading from Txt File in Swift 3
Accessibility Custom Actions Aren't Announced in Swift
Swift 4 - Hmcharacteristictypeserialnumber Deprecated
How to Present a UIcollectionview in Swiftui with UIviewcontrollerrepresentable
Enum of String Type Vs Struct with Static Constant
iOS Swift, Aws Cognito User Pool, Unable to Refresh Access Token
Implement Protocol with Different Associated Type
Pfobject Unable to Be Cast to Custom Subclass
"The Requested Snapshot Version Is Too Old." Error in Firestore
Swift Increment Int! Not Working
Difference Between Object(Forkey:) and Value(Forkey:) in Userdefaults
Swiftui CPU High Usage on Real-Time Foreach View Updating (Macos)
Cannot Convert Value of Type 'X' to Expected Argument Type 'X'
Uisearchcontroller Searchbar Misaligns While Active During Rotation
More Precision Than Double in Swift
How to Create Generic Convenience Initializer in Swift
Xcode Server: Opening Import File for Module 'Mobilecoreservices': Permission Denied