Skip to content

Instantly share code, notes, and snippets.

@fahied
Created April 9, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahied/d0db7ce5e78a95fe2189660a0e42eeab to your computer and use it in GitHub Desktop.
Save fahied/d0db7ce5e78a95fe2189660a0e42eeab to your computer and use it in GitHub Desktop.
Multi Line UIButton
import UIKit
class MultiLineButton: UIButton {
// MARK: - Init
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit() {
self.titleLabel?.numberOfLines = 0
self.titleLabel?.lineBreakMode = .byWordWrapping
}
// MARK: - Overrides
override var intrinsicContentSize: CGSize {
get {
return titleLabel?.intrinsicContentSize ?? CGSize.zero
}
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel?.preferredMaxLayoutWidth = titleLabel?.frame.size.width ?? 0
super.layoutSubviews()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment