Skip to content

Instantly share code, notes, and snippets.

@rockbruno
Last active September 7, 2018 06:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockbruno/042c02fb1e8776fc41930992f58027b6 to your computer and use it in GitHub Desktop.
Save rockbruno/042c02fb1e8776fc41930992f58027b6 to your computer and use it in GitHub Desktop.
"Native" Dual Label UIButton
extension UIButton {
/// Sets an UIButton's left and right title separated by invisible spaces, to give the impression of having two labels
/// without losing native UIButton properties, such as animations that come with the .system button type.
/// Requires tintColor to match the button's title color.
func setDualTitle(left: String, right: String, horizontalMargin: CGFloat = 8) {
let columnCount = (UIScreen.main.bounds.width - (horizontalMargin * 2)) * 0.2
let columns = String(repeating: "|", count: Int(columnCount))
let attributedString = NSMutableAttributedString(string: left, attributes: [.foregroundColor: tintColor])
attributedString.append(NSAttributedString(string: columns, attributes: [.foregroundColor: UIColor.clear]))
attributedString.append(NSAttributedString(string: right, attributes: [.foregroundColor: tintColor]))
setAttributedTitle(attributedString, for: .normal)
contentEdgeInsets = UIEdgeInsets(top: 0, left: horizontalMargin, bottom: 0, right: horizontalMargin)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment