Skip to content

Commit 043e050

Browse files
author
Anton Poltoratskyi
committed
Update Alert.swift
1 parent 2aa979e commit 043e050

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

NativeUI/Sources/Alert/Alert.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,57 @@ public struct Alert {
2828
}
2929
}
3030

31-
public let title: String?
32-
public let titleFont: UIFont
33-
public let message: String?
34-
public let messageFont: UIFont
31+
public enum Text {
32+
case string(String, UIFont)
33+
case attributedString(NSAttributedString)
34+
}
35+
36+
/// default font: `UIFont.systemFont(ofSize: 17, weight: .semibold)`
37+
public let title: Text?
38+
/// default font: `UIFont.systemFont(ofSize: 13, weight: .regular)`
39+
public let message: Text?
40+
/// Custom view to use instead of message or in addition to message.
3541
public let contentView: UIView?
42+
3643
public let tintColor: UIColor?
44+
3745
public let actions: [Action]
3846

39-
public init(title: String?,
40-
titleFont: UIFont = UIFont.systemFont(ofSize: 17, weight: .semibold),
41-
message: String?,
42-
messageFont: UIFont = UIFont.systemFont(ofSize: 13, weight: .regular),
47+
public init(title: Text?,
48+
message: Text?,
4349
contentView: UIView? = nil,
4450
tintColor: UIColor? = nil,
4551
actions: [Action]) {
4652
self.title = title
47-
self.titleFont = titleFont
4853
self.message = message
49-
self.messageFont = messageFont
5054
self.contentView = contentView
5155
self.tintColor = tintColor
5256
self.actions = actions
5357
}
58+
59+
public init(title: String?,
60+
titleFont: UIFont = UIFont.systemFont(ofSize: 17, weight: .semibold),
61+
message: String?,
62+
messageFont: UIFont = UIFont.systemFont(ofSize: 13, weight: .regular),
63+
contentView: UIView? = nil,
64+
tintColor: UIColor? = nil,
65+
actions: [Action]) {
66+
self.init(title: title.map { .string($0, titleFont) },
67+
message: message.map { .string($0, messageFont) },
68+
contentView: contentView,
69+
tintColor: tintColor,
70+
actions: actions)
71+
}
72+
73+
public init(title: NSAttributedString?,
74+
message: NSAttributedString?,
75+
contentView: UIView? = nil,
76+
tintColor: UIColor? = nil,
77+
actions: [Action]) {
78+
self.init(title: title.map { .attributedString($0) },
79+
message: message.map { .attributedString($0) },
80+
contentView: contentView,
81+
tintColor: tintColor,
82+
actions: actions)
83+
}
5484
}

NativeUI/Sources/Alert/AlertView.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,26 @@ final class AlertView: UIView {
156156
func setup(viewModel: Alert) {
157157
self.viewModel = viewModel
158158

159-
titleLabel.text = viewModel.title
160-
titleLabel.font = viewModel.titleFont
159+
switch viewModel.title {
160+
case let .string(text, font):
161+
titleLabel.text = text
162+
titleLabel.font = font
163+
case let .attributedString(attributedText):
164+
titleLabel.attributedText = attributedText
165+
case nil:
166+
titleLabel.text = nil
167+
}
161168
titleLabel.isHidden = viewModel.title == nil
162169

163-
messageLabel.text = viewModel.message
164-
messageLabel.font = viewModel.messageFont
170+
switch viewModel.message {
171+
case let .string(text, font):
172+
messageLabel.text = text
173+
messageLabel.font = font
174+
case let .attributedString(attributedText):
175+
messageLabel.attributedText = attributedText
176+
case nil:
177+
messageLabel.text = nil
178+
}
165179
messageLabel.isHidden = viewModel.message == nil
166180

167181
if let contentView = viewModel.contentView {

0 commit comments

Comments
 (0)