Skip to content

Instantly share code, notes, and snippets.

@jackd942
Created February 27, 2016 20:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jackd942/fbced96ac424927a697f to your computer and use it in GitHub Desktop.
Save jackd942/fbced96ac424927a697f to your computer and use it in GitHub Desktop.
Custom IBDesignable and IBInspectable UIButton created for use in the Devslopes App
//
// DAButton.swift
//
// Created by Jack Davis on 2/20/16.
// Copyright © 2016 Nine-42 LLC. All rights reserved.
//
// Custom UIButton class designed for Devslopes App.
//
//
import UIKit
import pop
@IBDesignable
class DAButton: UIButton {
// MARK: - Inspectables
@IBInspectable var colorCode: Int = 0 {
didSet {
self.backgroundColor = setColor(colorCode)
}
}
@IBInspectable var cornerRadius: CGFloat = 3.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
@IBInspectable var fontColor: UIColor = UIColor.whiteColor() {
didSet {
self.tintColor = fontColor
}
}
@IBInspectable var image: UIImage?
@IBInspectable var imgLeftInset: CGFloat = 10.0
@IBInspectable var imgTopInset: CGFloat = 7.0
@IBInspectable var imgBottomInset: CGFloat = 7.0
// MARK: - Properties
var buttonFont: String = "Noto Sans"
var imgView: UIImageView?
// MARK: - View
override func awakeFromNib() {
self.setupView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupView()
}
func setupView() {
self.backgroundColor = setColor(colorCode)
self.layer.cornerRadius = cornerRadius
self.layer.masksToBounds = true
if let font = UIFont(name: buttonFont, size: 14.0) {
self.titleLabel?.font = font
} else {
self.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 14.0)
}
self.setNeedsLayout()
self.setNeedsDisplay()
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
if self.image != nil {
self.imgView = UIImageView(frame: CGRectMake(self.imgLeftInset, self.imgTopInset, self.bounds.height - (self.imgTopInset + self.imgBottomInset), self.bounds.height - (self.imgTopInset + imgBottomInset)))
self.imgView!.contentMode = .ScaleAspectFit
self.imgView!.image = self.image
self.addSubview(self.imgView!)
}
}
//MARK: - Methods
func setColor(colorCode: Int) -> UIColor {
switch (colorCode) {
case 1:
return UIColor(netHex: 0x1FC299)
case 2:
return UIColor(netHex: 0xE9AA31)
case 3:
return UIColor(netHex: 0xF68A68)
case 4:
return UIColor(netHex: 0xB86044)
case 5:
return UIColor(netHex: 0x903E11)
case 6:
return UIColor(netHex: 0x3B5998)
case 7:
return UIColor(netHex: 0x000000)
case 8:
return UIColor(netHex: 0xFFFFFF)
case 9:
return UIColor(netHex: 0x707070)
default:
return UIColor(netHex: 0xD8471E)
}
}
}
@SketchySwift
Copy link

Thanks, awesome stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment