Created
February 2, 2016 00:11
-
-
Save kanekomasanori/2626434037ecee445f06 to your computer and use it in GitHub Desktop.
BorderView
This file contains 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
import UIKit | |
class BorderView: UIView { | |
struct BorderAttributes { | |
var draw = false | |
var width: CGFloat = 0.0 | |
var color = UIColor.clearColor() | |
} | |
var topBorderAttributes = BorderAttributes() | |
var rightBorderAttributes = BorderAttributes() | |
var bottomBorderAttributes = BorderAttributes() | |
var leftBorderAttributes = BorderAttributes() | |
override func drawRect(rect: CGRect) { | |
if topBorderAttributes.draw { | |
setBorderTop(topBorderAttributes.width, borderColor: topBorderAttributes.color) | |
} | |
if rightBorderAttributes.draw { | |
setBorderRight(rightBorderAttributes.width, borderColor: rightBorderAttributes.color) | |
} | |
if bottomBorderAttributes.draw { | |
setBorderBottom(bottomBorderAttributes.width, borderColor: bottomBorderAttributes.color) | |
} | |
if leftBorderAttributes.draw { | |
setBorderLeft(leftBorderAttributes.width, borderColor: leftBorderAttributes.color) | |
} | |
super.drawRect(rect) | |
} | |
func setBorder(borderWidth borderWidth: CGFloat, borderColor: UIColor?, borderRadius: CGFloat?) { | |
self.layer.borderWidth = borderWidth | |
self.layer.borderColor = borderColor?.CGColor | |
if let _ = borderRadius { | |
self.layer.cornerRadius = borderRadius! | |
} | |
self.layer.masksToBounds = true | |
} | |
func setBorderTop(borderWidth: CGFloat, borderColor: UIColor?) { | |
let line = setLine(borderColor) | |
line.frame = CGRectMake(0.0, 0.0, self.frame.width, borderWidth) | |
self.layer.addSublayer(line) | |
} | |
func setBorderRight(borderWidth: CGFloat, borderColor: UIColor?) { | |
let line = setLine(borderColor) | |
line.frame = CGRectMake(0.0, 0.0, borderWidth, self.frame.height) | |
self.layer.addSublayer(line) | |
} | |
func setBorderBottom(borderWidth: CGFloat, borderColor: UIColor?) { | |
let line = setLine(borderColor) | |
line.frame = CGRectMake(0.0, self.frame.height - borderWidth, self.frame.width, borderWidth) | |
self.layer.addSublayer(line) | |
} | |
func setBorderLeft(borderWidth: CGFloat, borderColor: UIColor?) { | |
let line = setLine(borderColor) | |
line.frame = CGRectMake(0.0, 0.0, borderWidth, self.frame.height) | |
self.layer.addSublayer(line) | |
} | |
// MARK : private func | |
private func setLine(borderColor: UIColor?) -> CALayer { | |
let line = CALayer() | |
self.layer.masksToBounds = true | |
if let _ = borderColor { | |
line.backgroundColor = borderColor!.CGColor | |
} else { | |
line.backgroundColor = UIColor.whiteColor().CGColor | |
} | |
return line | |
} | |
// MARK : @IBInspectable | |
@IBInspectable | |
var topBorderDraw: Bool { | |
get { | |
return topBorderAttributes.draw | |
} | |
set { | |
topBorderAttributes.draw = newValue | |
} | |
} | |
@IBInspectable | |
var topBorderWidth: CGFloat { | |
get { | |
return topBorderAttributes.width | |
} | |
set { | |
topBorderAttributes.width = newValue | |
} | |
} | |
@IBInspectable | |
var topBorderColor: UIColor { | |
get { | |
return topBorderAttributes.color | |
} | |
set { | |
topBorderAttributes.color = newValue | |
} | |
} | |
@IBInspectable | |
var rightBorderDraw: Bool { | |
get { | |
return rightBorderAttributes.draw | |
} | |
set { | |
rightBorderAttributes.draw = newValue | |
} | |
} | |
@IBInspectable | |
var rightBorderWidth: CGFloat { | |
get { | |
return rightBorderAttributes.width | |
} | |
set { | |
rightBorderAttributes.width = newValue | |
} | |
} | |
@IBInspectable | |
var rightBorderColor: UIColor { | |
get { | |
return rightBorderAttributes.color | |
} | |
set { | |
rightBorderAttributes.color = newValue | |
} | |
} | |
@IBInspectable | |
var bottomBorderDraw: Bool { | |
get { | |
return bottomBorderAttributes.draw | |
} | |
set { | |
bottomBorderAttributes.draw = newValue | |
} | |
} | |
@IBInspectable | |
var bottomBorderWidth: CGFloat { | |
get { | |
return bottomBorderAttributes.width | |
} | |
set { | |
bottomBorderAttributes.width = newValue | |
} | |
} | |
@IBInspectable | |
var bottomBorderColor: UIColor { | |
get { | |
return bottomBorderAttributes.color | |
} | |
set { | |
bottomBorderAttributes.color = newValue | |
} | |
} | |
@IBInspectable | |
var leftBorderDraw: Bool { | |
get { | |
return leftBorderAttributes.draw | |
} | |
set { | |
leftBorderAttributes.draw = newValue | |
} | |
} | |
@IBInspectable | |
var leftBorderWidth: CGFloat { | |
get { | |
return leftBorderAttributes.width | |
} | |
set { | |
leftBorderAttributes.width = newValue | |
} | |
} | |
@IBInspectable | |
var leftBorderColor: UIColor { | |
get { | |
return leftBorderAttributes.color | |
} | |
set { | |
leftBorderAttributes.color = newValue | |
} | |
} | |
// MARK : @IBInspectable | |
@IBInspectable | |
var borderWidth: CGFloat { | |
get { | |
return self.layer.borderWidth | |
} | |
set { | |
self.layer.borderWidth = newValue | |
} | |
} | |
@IBInspectable | |
var borderColor: UIColor? { | |
get { | |
if let _ = self.layer.borderColor { | |
return UIColor(CGColor: self.layer.borderColor!) | |
} | |
return nil | |
} | |
set { | |
self.layer.borderColor = newValue?.CGColor | |
} | |
} | |
@IBInspectable | |
var cornerRadius: CGFloat { | |
get { | |
return self.layer.cornerRadius | |
} | |
set { | |
self.layer.cornerRadius = newValue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment