-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBorders.swift
50 lines (44 loc) · 1.5 KB
/
Borders.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Created by Gustavo Nascimento on 9/14/16.
// Github: gustanas
// Twitter: @gusta_nas
import UIKit
enum Direction {
case up
case right
case bottom
case left
}
extension UIView {
func addBorder(direction: Direction, color: UIColor = UIColor(red: 0.78, green: 0.78, blue: 0.80, alpha: 1.0)) {
let width = CGFloat(1.0)
let border = CALayer()
border.borderColor = color.CGColor
let frameCalculator = frameCalculatorForDirection(direction)
border.frame = frameCalculator(self)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
private func frameCalculatorForDirection(direction: Direction) -> (UIView -> CGRect) {
let width = CGFloat(1.0)
switch direction {
case .up:
return { view in
return CGRect(x: 0, y: 0, width: view.frame.size.width, height: width)
}
case .right:
return { view in
return CGRect(x: view.frame.size.width - width, y: 0, width: width, height: view.frame.size.height)
}
case .bottom:
return { view in
return CGRect(x: 0, y: view.frame.size.height - width, width: view.frame.size.width, height: view.frame.size.height)
}
case .left:
return { view in
return CGRect(x: 0, y: 0, width: width, height: view.frame.size.height)
}
}
}
}