-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTPRadio.swift
156 lines (145 loc) · 6.49 KB
/
NTPRadio.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// @package NTPRadio.swift
// @author The Phuc
// @since 7/10/2016
import UIKit
class NTPRadio: UIView
{
private var SCREEN_WIDTH: CGFloat = CGFloat()
private var SCREEN_HEIGHT: CGFloat = CGFloat()
// define image for radio Button
private var checkedImage = UIImage(named: "Selected")! as UIImage
private var unCheckedImage = UIImage(named: "unSelected")! as UIImage
// define element for radio Button
let radio: UIButton = UIButton()
let titleDisplay: UILabel = UILabel()
// variable for save data for selected
private static var data:[String:String] = [String:String]()
private static var isSelected:Bool = false
private var splitStr = "####"
/* Init radio Button */
init(frame: CGRect, name: String, text: String, value: String, selected: Bool = false, titleColor: UIColor = UIColor(hue: 0.0389, saturation: 0, brightness: 0.36, alpha: 1.0))
{
super.init(frame: frame)
// get screen size
SCREEN_WIDTH = UIScreen.mainScreen().bounds.width
SCREEN_HEIGHT = UIScreen.mainScreen().bounds.height
// config radio button
radio.frame = CGRectMake(0, 0, frame.height, frame.height)
if NTPRadio.isSelected == false {
if selected {
radio.setBackgroundImage(checkedImage, forState: .Normal)
NTPRadio.data[name] = value
NTPRadio.isSelected = true
} else {
radio.setBackgroundImage(unCheckedImage, forState: .Normal)
}
} else {
radio.setBackgroundImage(unCheckedImage, forState: .Normal)
}
radio.titleLabel?.text = name + splitStr + value
radio.addTarget(self, action: "tappedButton:", forControlEvents: UIControlEvents.TouchUpInside)
// config text display
let heightLine = heightLabel(text, font: UIFont(name: "Arial", size: frame.height - 7)!, width: frame.width - frame.height - 5)
titleDisplay.frame = CGRectMake(frame.height + 7, 0, frame.width - frame.height - 5, heightLine)
titleDisplay.text = text
titleDisplay.font = UIFont(name: "Arial", size: frame.height - 7)
titleDisplay.textColor = titleColor
titleDisplay.lineBreakMode = NSLineBreakMode.ByWordWrapping
titleDisplay.numberOfLines = 0
// event when tap to label
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tappedLabel:")
tapGesture.numberOfTapsRequired = 1
titleDisplay.userInteractionEnabled = true
titleDisplay.addGestureRecognizer(tapGesture)
// add element to UIView
self.addSubview(radio)
self.addSubview(titleDisplay)
}
/* caculate height of title display */
private func heightLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
/* event when tap to radio label */
func tappedLabel(sender: UILabel)
{
let subperView = super.subviews
var nameRadio: String = ""
var currentSelected: String = ""
for sub in subperView {
if Mirror(reflecting: sub).subjectType == UIButton.self {
let button = sub as! UIButton
let titleLabel = button.titleLabel?.text
let titleLabelArray = titleLabel?.componentsSeparatedByString(splitStr)
nameRadio = titleLabelArray![0]
currentSelected = titleLabelArray![1]
}
}
for sub in (self.superview?.subviews)! {
if Mirror(reflecting: sub).subjectType == NTPRadio.self {
for subRadio in sub.subviews {
if Mirror(reflecting: subRadio).subjectType == UIButton.self {
let button = subRadio as! UIButton
let titleLabel = button.titleLabel?.text
let titleLabelArray = titleLabel?.componentsSeparatedByString(splitStr)
if nameRadio == titleLabelArray![0] as String {
if currentSelected == titleLabelArray![1] as String {
button.setBackgroundImage(checkedImage, forState: .Normal)
NTPRadio.data[nameRadio] = currentSelected
} else {
button.setBackgroundImage(unCheckedImage, forState: .Normal)
}
}
}
}
}
}
}
/* event when tap to radio button */
func tappedButton(sender: UIButton)
{
let dataSelected = sender.titleLabel?.text
let dataSelectedArray = dataSelected?.componentsSeparatedByString(splitStr)
let nameRadio = dataSelectedArray![0] as String
let currentSelected = dataSelectedArray![1] as String
for sub in (self.superview?.subviews)! {
if Mirror(reflecting: sub).subjectType == NTPRadio.self {
for subRadio in sub.subviews {
if Mirror(reflecting: subRadio).subjectType == UIButton.self {
let button = subRadio as! UIButton
let titleLabel = button.titleLabel?.text
let titleLabelArray = titleLabel?.componentsSeparatedByString(splitStr)
if nameRadio == titleLabelArray![0] as String {
if currentSelected == titleLabelArray![1] as String {
button.setBackgroundImage(checkedImage, forState: .Normal)
NTPRadio.data[nameRadio] = currentSelected
} else {
button.setBackgroundImage(unCheckedImage, forState: .Normal)
}
}
}
}
}
}
}
/* get data radio @params: name */
static func getSelected(name:String) -> String
{
if NTPRadio.data[name] != nil {
return NTPRadio.data[name]! as String
}
return ""
}
/* destruct radio */
deinit {
NTPRadio.data.removeAll()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}