Skip to content

Instantly share code, notes, and snippets.

@leoiphonedev
Last active July 14, 2019 07:29
Show Gist options
  • Save leoiphonedev/ba1fdb4c18ffee011ae6789368606ca1 to your computer and use it in GitHub Desktop.
Save leoiphonedev/ba1fdb4c18ffee011ae6789368606ca1 to your computer and use it in GitHub Desktop.
AdaptableSizeButton class enables the size of button as per the title set to UIButton via overriding intrinsicContentSize of UIButton
class AdaptableSizeButton: UIButton {
override var intrinsicContentSize: CGSize {
let labelSize = titleLabel?.sizeThatFits(CGSize(width: frame.size.width, height: CGFloat.greatestFiniteMagnitude)) ?? .zero
let desiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)
return desiredButtonSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment