-
Notifications
You must be signed in to change notification settings - Fork 0
/
BorderedButton.swift
81 lines (62 loc) · 2.18 KB
/
BorderedButton.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// BorderedButton.swift
// BorderedButton
//
// Created by Eugene on 7/20/14.
// Copyright (c) 2014 33Devs Consulting. All rights reserved.
//
import UIKit
import QuartzCore
class BorderedButton: UIButton {
private var circleLayer: CAShapeLayer = CAShapeLayer()
private var borderColor: UIColor = UIColor.whiteColor()
init(frame: CGRect) {
super.init(frame: frame)
setupButton()
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
setupButton()
}
private func setupButton() {
self.borderColor = self.titleLabel.textColor
self.circleLayer.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)
self.circleLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))
var path: UIBezierPath
if (self.frame.size.width == self.frame.size.height) {
path = UIBezierPath(ovalInRect: CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)));
}
else {
path = UIBezierPath(roundedRect: self.bounds, cornerRadius: 8)
}
self.circleLayer.path = path.CGPath;
self.circleLayer.strokeColor = self.borderColor.CGColor;
self.circleLayer.lineWidth = 2.0;
self.circleLayer.fillColor = nil;
self.layer.insertSublayer(self.circleLayer, below: self.titleLabel.layer)
}
override var highlighted: Bool {
get {
return super.highlighted
}
set(isHighlighted) {
if (isHighlighted) {
self.circleLayer.fillColor = self.borderColor.CGColor
self.titleLabel.textColor = UIColor.whiteColor()
}
else {
self.circleLayer.fillColor = nil
self.titleLabel.textColor = self.borderColor
}
}
}
override func setTitleColor(color: UIColor!, forState state: UIControlState) {
super.setTitleColor(color, forState: state)
self.borderColor = color
self.circleLayer.borderColor = color.CGColor
}
override func layoutSubviews() {
super.layoutSubviews()
setupButton()
}
}