coopersita
coopersita

Reputation: 5041

How to customize uibutton's class in Swift?

I have different types of buttons in my app, and I would like to have a way so that I don't have to write the code to format the attributed labels or setup insets and background image in interface builder for each button on every view controller.

I was thinking of doing something like this:

import UIKit

class whiteButton: UIButton {

    required init(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        if traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Regular {
            contentEdgeInsets.top = 16
            contentEdgeInsets.right = 47
            contentEdgeInsets.bottom = 16
            contentEdgeInsets.left = 47
        } else {
            contentEdgeInsets.top = 11
            contentEdgeInsets.right = 35
            contentEdgeInsets.bottom = 11
            contentEdgeInsets.left = 35
        }

        let imageInsets : UIEdgeInsets = UIEdgeInsetsMake(4, 4, 4, 4)

        var image = UIImage(named: "white-button.png")
        image = image!.resizableImageWithCapInsets(imageInsets)

        setBackgroundImage(image, forState: .Normal)

        var formattedString = NSMutableAttributedString(string: attributedTitleForState(.Normal)!.string)

        let fontDescriptor = UIFontDescriptor(fontAttributes: [UIFontDescriptorNameAttribute : "AvenirNext-Bold"])
        var font : UIFont

        if  traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Regular {
            font = UIFont(descriptor: fontDescriptor, size: 18)
        } else {
            font = UIFont(descriptor: fontDescriptor, size: 12)
        }
        let attributes = [NSFontAttributeName : font]

        formattedString.addAttributes(attributes, range: NSMakeRange(0, formattedString.length))
        formattedString.addAttribute(NSKernAttributeName, value: 1.5, range: NSMakeRange(0, formattedString.length))
        formattedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, formattedString.length))

        setAttributedTitle(formattedString, forState: .Normal)

    }

}

This sort of works, but I have the following questions:

  1. Is this a good way of doing it? I would have maybe 3 different types of buttons, so I'd have a subclass for each one.
  2. The use of the traitCollection to determine the size class is not working. How could I fix it (I tried accessing the superview to obtain it, but it was nil)? I want to be able to give a different font size, and insets, based on the size class.

Upvotes: 2

Views: 2664

Answers (1)

Schemetrical
Schemetrical

Reputation: 5536

The best way is that you make a generic button class for all your different colored buttons. After you init the new button, you can set the color.

Another way is to use a designated initialiser with a UIColor so that your code adapts to the color you feed to it (init(color:UIColor)).

You can access the trait collection in didMoveToSuperview.

Upvotes: 1

Related Questions