Add letters to dialpad buttons

This commit is contained in:
ismailgulek
2021-02-10 16:17:38 +03:00
parent bc43c02908
commit 50a9579fae
3 changed files with 86 additions and 5 deletions
@@ -19,8 +19,25 @@ import UIKit
/// Digit button class for Dialpad screen
class DialpadButton: UIButton {
struct ViewData {
var title: String
var subtitle: String?
var showsSubtitleSpace: Bool
init(title: String, subtitle: String? = nil, showsSubtitleSpace: Bool = false) {
self.title = title
self.subtitle = subtitle
self.showsSubtitleSpace = showsSubtitleSpace
}
}
private var viewData: ViewData?
private var theme: Theme = ThemeService.shared().theme
private enum Constants {
static let size: CGSize = CGSize(width: 68, height: 68)
static let titleFont: UIFont = .boldSystemFont(ofSize: 32)
static let subtitleFont: UIFont = .boldSystemFont(ofSize: 12)
}
init() {
@@ -40,6 +57,31 @@ class DialpadButton: UIButton {
private func setup() {
clipsToBounds = true
layer.cornerRadius = Constants.size.width/2
vc_enableMultiLinesTitle()
}
func render(withViewData viewData: ViewData) {
self.viewData = viewData
let totalAttributedString = NSMutableAttributedString(string: viewData.title,
attributes: [
.font: Constants.titleFont,
.foregroundColor: theme.textPrimaryColor
])
if let subtitle = viewData.subtitle {
totalAttributedString.append(NSAttributedString(string: "\n" + subtitle, attributes: [
.font: Constants.subtitleFont,
.foregroundColor: theme.textPrimaryColor
]))
} else if viewData.showsSubtitleSpace {
totalAttributedString.append(NSAttributedString(string: "\n ", attributes: [
.font: Constants.subtitleFont,
.foregroundColor: theme.textPrimaryColor
]))
}
setAttributedTitle(totalAttributedString, for: .normal)
}
}
@@ -49,8 +91,14 @@ class DialpadButton: UIButton {
extension DialpadButton: Themable {
func update(theme: Theme) {
setTitleColor(theme.textPrimaryColor, for: .normal)
self.theme = theme
backgroundColor = theme.headerBackgroundColor
// re-render view data if set
if let viewData = self.viewData {
render(withViewData: viewData)
}
}
}