Instantly share code, notes, and snippets.
Created
February 27, 2016 20:41
-
Star
3
(3)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save jackd942/9812092f30f96b80820a to your computer and use it in GitHub Desktop.
Custom IBDesignable and IBInspectable Polygon UIButton created for use in the Devslopes App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DAPolyButton.swift | |
// | |
// Created by Jack Davis on 2/21/16. | |
// Copyright © 2016 Nine-42 LLC. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable | |
class DAPolyButton: UIButton { | |
// MARK: - Inspectable | |
@IBInspectable var colorCode: Int = 0 { | |
didSet { | |
self.bgColor = setColor(colorCode) | |
} | |
} | |
@IBInspectable var numberOfSides: Int = 6 | |
@IBInspectable var fontColor: UIColor = UIColor.whiteColor() { | |
didSet { | |
self.tintColor = fontColor | |
} | |
} | |
@IBInspectable var image: UIImage? = nil { | |
didSet { | |
if let img = image { | |
self.imageView?.contentMode = .ScaleAspectFit | |
self.setImage(img, forState: .Normal) | |
} | |
} | |
} | |
@IBInspectable var imgTopInset: CGFloat = 5.0 { | |
didSet { | |
self.imageEdgeInsets = UIEdgeInsetsMake(imgTopInset, imgLeftInset, imgBottomInset, imgRightInset) | |
} | |
} | |
@IBInspectable var imgLeftInset: CGFloat = 2.0 { | |
didSet { | |
self.imageEdgeInsets = UIEdgeInsetsMake(imgTopInset, imgLeftInset, imgBottomInset, imgRightInset) | |
} | |
} | |
@IBInspectable var imgBottomInset: CGFloat = 5.0 { | |
didSet { | |
self.imageEdgeInsets = UIEdgeInsetsMake(imgTopInset, imgLeftInset, imgBottomInset, imgRightInset) | |
} | |
} | |
@IBInspectable var imgRightInset: CGFloat = 0.0 { | |
didSet { | |
self.imageEdgeInsets = UIEdgeInsetsMake(imgTopInset, imgLeftInset, imgBottomInset, imgRightInset) | |
} | |
} | |
// MARK: - Properties | |
var bgColor: UIColor = UIColor(netHex: 0xd8471e) | |
var buttonFont: String = "Noto Sans" | |
// MARK: - View | |
override func awakeFromNib() { | |
self.setupView() | |
} | |
override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
self.setupView() | |
} | |
func setupView() { | |
self.setColor(colorCode) | |
if let font = UIFont(name: buttonFont, size: 14.0) { | |
self.titleLabel?.font = font | |
} else { | |
self.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 14.0) | |
} | |
if let img = image { | |
self.imageView?.contentMode = UIViewContentMode.ScaleAspectFit | |
self.imageEdgeInsets = UIEdgeInsetsMake(imgTopInset, imgLeftInset, imgBottomInset, imgRightInset) | |
self.setImage(img, forState: .Normal) | |
} | |
self.setNeedsLayout() | |
} | |
// MARK: - Overrides | |
override func drawRect(rect:CGRect) { | |
drawPolygonBezier(CGRectGetMidX(rect), y: CGRectGetMidY(rect), radius: CGRectGetWidth(rect)/2, sides: numberOfSides, color: bgColor, offset:0) | |
if let img = image { | |
self.setImage(img, forState: .Normal) | |
} | |
} | |
// MARK: - Methods | |
func polygonPointArray(sides: Int, x: CGFloat, y: CGFloat, radius: CGFloat, offset: CGFloat) -> [CGPoint] { | |
let angle = (360/CGFloat(sides)).radians() | |
let cx = x // x origin | |
let cy = y // y origin | |
let r = radius // radius of circle | |
var i = 0 | |
var points = [CGPoint]() | |
while i <= sides { | |
let xpoint = cx + r * cos(angle * CGFloat(i) - offset.radians()) | |
let ypoint = cy + r * sin(angle * CGFloat(i) - offset.radians()) | |
points.append(CGPoint(x: xpoint, y: ypoint)) | |
i++ | |
} | |
return points | |
} | |
func polygonPath(x: CGFloat, y: CGFloat, radius: CGFloat, sides: Int, offset: CGFloat) -> CGPathRef { | |
let path = CGPathCreateMutable() | |
let points = polygonPointArray(sides, x: x, y: y, radius: radius, offset: offset) | |
let cgp = points[0] | |
CGPathMoveToPoint(path, nil, cgp.x, cgp.y) | |
for p in points { | |
CGPathAddLineToPoint(path, nil, p.x, p.y) | |
} | |
CGPathCloseSubpath(path) | |
return path | |
} | |
func drawPolygonBezier(x: CGFloat, y: CGFloat, radius: CGFloat, sides: Int, color: UIColor, offset: CGFloat) -> UIBezierPath { | |
let path = polygonPath(x, y: y, radius: radius, sides: sides, offset: offset) | |
let bezier = UIBezierPath(CGPath: path) | |
color.setFill() | |
bezier.fill() | |
return bezier | |
} | |
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) | |
default: | |
return UIColor(netHex: 0xD8471E) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment