Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
VerticalLayoutButton: align UIButtons' imageView and titleLabel verticaly
import UIKit
@IBDesignable class VerticalLayoutButton: UIButton {
@IBInspectable var normalColor: UIColor = ColorConstants.SUB_COLOR
@IBInspectable var selectedColor: UIColor = ColorConstants.MAIN_COLOR
@IBInspectable var verticalSpacing: CGFloat = 0.0 {
didSet {
guard let titleLabel: UILabel = titleLabel else { return }
guard let currentTitle: String = currentTitle else { return }
guard let imageView: UIImageView = imageView else { return }
guard let image: UIImage = imageView.image else { return }
let labelString = NSString(string: currentTitle)
let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font])
let imageSize: CGSize = image.size
titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + verticalSpacing), 0.0)
imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + verticalSpacing), 0.0, 0.0, -titleSize.width)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment