@@ -19,10 +19,30 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
19
19
@IBOutlet public var iconContainer : UIView ?
20
20
@IBOutlet public var iconImageView : UIImageView ?
21
21
@IBOutlet public var iconLabel : UILabel ?
22
- @IBOutlet public var button : UIButton ?
23
22
@IBOutlet public var contentView : UIView ?
24
23
@IBOutlet public var backgroundView : UIView !
25
24
25
+ @IBOutlet public var button : UIButton ? {
26
+ didSet {
27
+ if let old = oldValue {
28
+ old. removeTarget ( self , action: #selector( MessageView . buttonTapped ( _: ) ) , forControlEvents: . TouchUpInside)
29
+ }
30
+ if let button = button {
31
+ button. addTarget ( self , action: #selector( MessageView . buttonTapped ( _: ) ) , forControlEvents: . TouchUpInside)
32
+ }
33
+ }
34
+ }
35
+
36
+ /*
37
+ MARK: - Button tap handler
38
+ */
39
+
40
+ public var buttonTapHandler : ( ( button: UIButton ) -> Void ) ?
41
+
42
+ func buttonTapped( button: UIButton ) {
43
+ buttonTapHandler ? ( button: button)
44
+ }
45
+
26
46
/*
27
47
MARK: - Creating message views
28
48
*/
@@ -41,23 +61,60 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
41
61
public static func viewFromNib< T: MessageView > ( layout layout: Layout , bundle: NSBundle ) -> T {
42
62
return try ! UIView . viewFromNib ( named: layout. rawValue, bundle: bundle)
43
63
}
44
-
64
+
45
65
/*
46
- MARK: - Configuring the theme
66
+ MARK: - Initialization
47
67
*/
68
+
69
+ public required init ? ( coder aDecoder: NSCoder ) {
70
+ super. init ( coder: aDecoder)
71
+ backgroundView = self
72
+ }
73
+
74
+ public override init ( frame: CGRect ) {
75
+ super. init ( frame: frame)
76
+ backgroundView = self
77
+ }
78
+
79
+ public override func awakeFromNib( ) {
80
+ layoutMargins = UIEdgeInsetsZero
81
+ }
82
+
83
+ /*
84
+ MARK: - Identifiable
85
+ */
86
+
87
+ public var id : String {
88
+ return " MessageView:title= \( titleLabel? . text) , body= \( bodyLabel? . text) "
89
+ }
90
+
91
+ /*
92
+ MARK: - MarginAdjustable
93
+ */
94
+
95
+ @IBInspectable public var bounceAnimationOffset : CGFloat = 5.0
96
+
97
+ @IBInspectable public var statusBarOffset : CGFloat = 20.0
98
+ }
48
99
100
+ /*
101
+ MARK: - Configuring the theme
102
+ */
103
+
104
+ extension MessageView {
105
+
49
106
public func configureErrorTheme( ) {
50
107
let backgroundColor = UIColor ( red: 249.0 / 255.0 , green: 66.0 / 255.0 , blue: 47.0 / 255.0 , alpha: 1.0 )
51
108
let foregroundColor = UIColor . whiteColor ( )
52
109
configureTheme ( backgroundColor: backgroundColor, foregroundColor: foregroundColor, iconImage: Icon . Error. image)
53
110
}
54
-
111
+
55
112
public func configureWarningTheme( ) {
56
113
let backgroundColor = UIColor ( red: 238.0 / 255.0 , green: 189.0 / 255.0 , blue: 34.0 / 255.0 , alpha: 1.0 )
57
114
let foregroundColor = UIColor . whiteColor ( )
58
115
configureTheme ( backgroundColor: backgroundColor, foregroundColor: foregroundColor, iconImage: Icon . Warning. image)
59
116
}
60
-
117
+
61
118
public func configureInfoTheme( ) {
62
119
let backgroundColor = UIColor ( red: 225.0 / 255.0 , green: 225.0 / 255.0 , blue: 225.0 / 255.0 , alpha: 1.0 )
63
120
let foregroundColor = UIColor . darkTextColor ( )
@@ -78,15 +135,18 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
78
135
button? . contentEdgeInsets = UIEdgeInsetsMake ( 7.0 , 7.0 , 7.0 , 7.0 )
79
136
button? . layer. cornerRadius = 5.0
80
137
}
81
-
138
+
82
139
public override func configureDropShadow( ) {
83
140
configureDropShadow ( backgroundView ?? self )
84
141
}
85
-
86
- /*
87
- MARK: - Configuring the content
88
- */
142
+ }
143
+
144
+ /*
145
+ MARK: - Configuring the content
146
+ */
89
147
148
+ extension MessageView {
149
+
90
150
public func configureContent( body body: String ) {
91
151
bodyLabel? . text = body
92
152
}
@@ -101,54 +161,20 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
101
161
iconImageView? . image = iconImage
102
162
iconLabel? . text = nil
103
163
}
104
-
164
+
105
165
public func configureContent( title title: String , body: String , iconText: String ) {
106
166
configureContent ( title: title, body: body)
107
167
iconImageView? . image = nil
108
168
iconLabel? . text = iconText
109
169
}
110
-
111
- public func configureContent( title title: String ? , body: String ? , iconImage: UIImage ? , iconText: String ? , buttonImage: UIImage ? , buttonTitle: String ? , buttonHandler : ( ( ) -> Void ) ? ) {
170
+
171
+ public func configureContent( title title: String ? , body: String ? , iconImage: UIImage ? , iconText: String ? , buttonImage: UIImage ? , buttonTitle: String ? , buttonTapHandler : ( ( button : UIButton ) -> Void ) ? ) {
112
172
titleLabel? . text = title
113
173
bodyLabel? . text = body
114
174
iconImageView? . image = iconImage
115
175
iconLabel? . text = iconText
116
176
button? . setImage ( buttonImage, forState: . Normal)
117
177
button? . setTitle ( buttonTitle, forState: . Normal)
118
- // TODO set button tap handler
119
- }
120
-
121
- /*
122
- MARK: - Initialization
123
- */
124
-
125
- public required init ? ( coder aDecoder: NSCoder ) {
126
- super. init ( coder: aDecoder)
127
- backgroundView = self
128
- }
129
-
130
- public override init ( frame: CGRect ) {
131
- super. init ( frame: frame)
132
- backgroundView = self
133
- }
134
-
135
- public override func awakeFromNib( ) {
136
- layoutMargins = UIEdgeInsetsZero
178
+ self . buttonTapHandler = buttonTapHandler
137
179
}
138
-
139
- /*
140
- MARK: - Identifiable
141
- */
142
-
143
- public var id : String {
144
- return " MessageView:title= \( titleLabel? . text) , body= \( bodyLabel? . text) "
145
- }
146
-
147
- /*
148
- MARK: - MarginAdjustable
149
- */
150
-
151
- @IBInspectable public var bounceAnimationOffset : CGFloat = 5.0
152
-
153
- @IBInspectable public var statusBarOffset : CGFloat = 20.0
154
180
}
0 commit comments