Skip to content

Instantly share code, notes, and snippets.

@alanf
Last active February 22, 2018 21:28
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 alanf/774957d6c56065bde8f04c89a2f7d097 to your computer and use it in GitHub Desktop.
Save alanf/774957d6c56065bde8f04c89a2f7d097 to your computer and use it in GitHub Desktop.
import UIKit
@IBDesignable class RoundedButton: UIButton {
// https://stackoverflow.com/questions/29618760/create-a-rectangle-with-just-two-rounded-corners-in-swift
override func awakeFromNib() {
super.awakeFromNib()
let roundedButtonFont = UIFont(name: "Montserrat-Bold", size: 14) ?? UIFont.boldSystemFont(ofSize: 14)
titleLabel?.font = roundedButtonFont
roundedLayer.strokeColor = Sport.Color.Green.cgColor
}
fileprivate let roundedLayer = CAShapeLayer()
override func layoutSubviews() {
super.layoutSubviews()
clipsToBounds = false
roundedLayer.borderColor = Sport.Color.Green.cgColor
let r = self.bounds.size.height / 2
let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: r)
roundedLayer.path = path.cgPath
layer.insertSublayer(roundedLayer, at: 0)
}
}
// TODO: these should be renamed. The secondary button style is now the primary style.
@IBDesignable class RoundedButtonPrimary: RoundedButton {
override func awakeFromNib() {
super.awakeFromNib()
roundedLayer.fillColor = UIColor.clear.cgColor
}
}
@IBDesignable class RoundedButtonSecondary: RoundedButton {
override func awakeFromNib() {
super.awakeFromNib()
roundedLayer.fillColor = Sport.Color.Green.cgColor
}
}
//@IBDesignable class CaretButton: UIButton {
class CaretButton: UIButton {
@IBInspectable var fontSize: CGFloat = 16.0
@IBInspectable var fontFamily: String = "Montserrat-Medium"
@IBInspectable var caretFontSize: CGFloat = 24.0
@IBInspectable var caretFontFamily: String = "Montserrat-Medium"
@IBInspectable var caretVerticalOffset: CGFloat = -3.0
override func awakeFromNib() {
super.awakeFromNib()
let font = UIFont(name: fontFamily, size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)
let caretFont = UIFont(name: caretFontFamily, size: caretFontSize) ?? UIFont.systemFont(ofSize: fontSize)
if let attrString = attributedTitle(for: .normal)?.mutableCopy() as? NSMutableAttributedString {
var caretRange: NSRange
var textRange: NSRange
if attrString.string.hasPrefix("<") { // As in "< Back"
caretRange = NSRange(location: 0, length: 1)
textRange = NSRange(location: 1, length: attrString.length-1)
} else if attrString.string.hasSuffix(">") { // As in "Next >"
textRange = NSRange(location: 0, length: attrString.length-1)
caretRange = NSRange(location: attrString.length-1, length: 1)
} else {
fatalError()
}
attrString.addAttribute(NSAttributedStringKey.font, value: font, range: textRange)
attrString.addAttribute(NSAttributedStringKey.baselineOffset, value: caretVerticalOffset, range: caretRange)
attrString.addAttribute(NSAttributedStringKey.font, value: caretFont, range: caretRange)
setAttributedTitle(attrString.copy() as? NSAttributedString, for: .normal)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment