Autodetect constrained NSButton text inserts

I'm on OSX (not iOS), Xcode 8.2, Objective-C

I have two constrained buttons. Both buttons have centered headings and both have the same width (via constraints). No, when it comes to a different language, the buttons get wider to fit longer text. But I would like to have some space between the border of the button and the text. IB insert options do not exist for NSButtons and I have not found any equivalent solutions.

Any help is appreciated

enter image description here

Restrictions for the bottom button: ("New Project" - top button)

enter image description here

+3


source to share


2 answers


You have to override the readinly intrinsicContentSize property the same as in UILabel

Here is an example of overriding NSButton with properties that you can set in the xib / storyboard file using the attribute pointer



//CustomButton.h file

@interface CustomButton : NSButton

@property (nonatomic, assign) IBInspectable CGFloat horizontalPadding;
@property (nonatomic, assign) IBInspectable CGFloat verticalPadding;

@end

//CustomButton.m file

@implementation CustomButton

- (NSSize) intrinsicContentSize{
    CGSize size = [super intrinsicContentSize];
    size.width += self.horizontalPadding;
    size.height += self.verticalPadding;
    return size;
}

@end

      

Happy coding 👨💻

+1


source


Swift 4



class Button: NSButton {

    @IBInspectable var paddingLeft   : CGFloat = 0
    @IBInspectable var paddingTop    : CGFloat = 0
    @IBInspectable var paddingBottom : CGFloat = 0
    @IBInspectable var paddingRight  : CGFloat = 0

    override var alignmentRectInsets: NSEdgeInsets {
        var insets    = super.alignmentRectInsets
        insets.left   = -self.paddingLeft
        insets.top    = -self.paddingTop
        insets.right  = -self.paddingRight
        insets.bottom = -self.paddingBottom
        return insets
    }
}

      

0


source







All Articles