Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class CentralMenuButton: UIButton {
var label: UILabel?
var labelText: String!
var labelColor: UIColor!
var labelFont: UIFont!
var labelAlignment: NSTextAlignment!
override init(frame: CGRect) {
super.init(frame: frame)
self.setupButton()
}
func setupButton() {
self.layer.cornerRadius = 10
self.labelText = self.titleLabel?.text
self.labelColor = self.titleLabel?.textColor
self.labelAlignment = self.titleLabel?.textAlignment
self.labelFont = self.titleLabel?.font
self.setTitle("", for: .normal)
if self.labelText != nil {
self.setLabel(text: labelText)
}
}
func setLabel(text: String) {
if self.label == nil {
let frame = CGRect(x: 8, y: 8, width: self.frame.width, height: 35)
self.label = UILabel(frame: frame)
self.label?.font = self.labelFont
self.label?.textColor = self.labelColor
self.label?.textAlignment = self.labelAlignment
self.addSubview(label!)
}
label?.text = text
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupButton()
}
}
class SecondaryMenuButton: UIButton {
var label: UILabel?
var labelText: String!
var labelColor: UIColor!
var labelFont: UIFont!
var labelAlignment: NSTextAlignment!
override init(frame: CGRect) {
super.init(frame: frame)
self.setupButton()
}
func setupButton() {
self.layer.cornerRadius = 10
self.labelText = self.titleLabel?.text
self.labelColor = self.titleLabel?.textColor
self.labelAlignment = self.titleLabel?.textAlignment
self.labelFont = self.titleLabel?.font
self.setTitle("", for: .normal)
if self.labelText != nil {
self.setLabel(text: labelText)
}
}
func setTextColor(color: UIColor) {
setupLabel()
label?.textColor = color
}
func setTextAlignment(alignment: NSTextAlignment) {
setupLabel()
label?.textAlignment = alignment
}
private func setupLabel() {
if self.label == nil {
let frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
self.label = UILabel(frame: frame)
self.label?.font = self.labelFont
self.label?.textColor = self.labelColor
self.label?.textAlignment = self.labelAlignment
self.addSubview(label!)
}
}
func setLabel(text: String) {
setupLabel()
label?.text = text
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupButton()
}
}
class BasicRoundedButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.setupButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupButton()
}
func setupButton() {
self.layer.cornerRadius = 10
}
}
class BasicRoundedSwitch: UIButton {
var statusLabel: UILabel?
var label: UILabel?
var labelText: String!
var labelColor: UIColor!
var labelFont: UIFont!
var labelAlignment: NSTextAlignment!
@IBInspectable var onColor: UIColor = UIColor.green
@IBInspectable var offColor: UIColor = UIColor.red
@IBInspectable var offLabelText: String = "Off"
@IBInspectable var onLabelText: String = "On"
@IBInspectable var switchState: Bool = false {
didSet {
self.updateLabel()
}
}
func updateLabel() {
if switchState == false {
self.statusLabel?.text = self.offLabelText
self.updateBackgroundColor(color: self.offColor)
} else {
self.statusLabel?.text = self.onLabelText
self.updateBackgroundColor(color: self.onColor)
}
}
private func updateBackgroundColor(color: UIColor){
UIView.animate(withDuration: 0.3) {
self.backgroundColor = color
}
}
private func setupLabel() {
if self.statusLabel == nil {
let frame = CGRect(x: 8, y: 38, width: self.frame.width, height: 30)
statusLabel = UILabel.init(frame: frame)
statusLabel?.textColor = self.labelColor
statusLabel?.font = UIFont.systemFont(ofSize: 25)
self.updateLabel()
self.addSubview(statusLabel!)
}
if self.label == nil {
let frame = CGRect(x: 8, y: 8, width: self.frame.width, height: 30)
label = UILabel(frame: frame)
label?.font = self.labelFont
label?.preferredMaxLayoutWidth = self.frame.width
label?.textColor = self.labelColor
label?.textAlignment = self.labelAlignment
label?.numberOfLines = 0
label?.lineBreakMode = .byWordWrapping
let widthConstraint = NSLayoutConstraint(item: label!, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: 0)
self.addConstraint(widthConstraint)
self.addSubview(label!)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setupButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupButton()
}
@objc func toggled() {
if self.switchState == false {
self.switchState = true
} else {
self.switchState = false
}
self.updateLabel()
}
func setLabel(text: String) {
setupLabel()
label?.text = text
}
func setupButton() {
self.labelText = self.titleLabel?.text
self.labelColor = self.titleLabel?.textColor
self.labelAlignment = self.titleLabel?.textAlignment
self.labelFont = self.titleLabel?.font
self.layer.cornerRadius = 10
self.setupLabel()
self.setTitle("", for: .normal)
if self.labelText != nil {
self.setLabel(text: labelText)
}
self.addTarget(self, action: #selector(toggled), for: .touchDown)
}
}
class CentralMenuContainerView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.cornerRadius = 10
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.cornerRadius = 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment