|
| 1 | +import UIKit |
| 2 | + |
| 3 | +/// A view with an injected content and a description withl highlighted words |
| 4 | +class MigrationCenterView: UIView { |
| 5 | + |
| 6 | + private let contentView: UIView |
| 7 | + |
| 8 | + private let descriptionText: String |
| 9 | + |
| 10 | + private let highlightedDescriptionText: String |
| 11 | + |
| 12 | + private lazy var descriptionLabel: UILabel = { |
| 13 | + let label = UILabel() |
| 14 | + label.font = WPStyleGuide.fontForTextStyle(.footnote, fontWeight: .regular) |
| 15 | + label.attributedText = Appearance.highlightString(highlightedDescriptionText, inString: descriptionText) |
| 16 | + label.textColor = Appearance.descriptionTextColor |
| 17 | + label.numberOfLines = 0 |
| 18 | + return label |
| 19 | + }() |
| 20 | + |
| 21 | + private lazy var mainStackView: UIStackView = { |
| 22 | + let stackView = UIStackView(arrangedSubviews: [contentView, descriptionLabel]) |
| 23 | + stackView.axis = .vertical |
| 24 | + stackView.alignment = .center |
| 25 | + stackView.setCustomSpacing(Appearance.fakeAlertToDescriptionSpacing, after: contentView) |
| 26 | + stackView.translatesAutoresizingMaskIntoConstraints = false |
| 27 | + return stackView |
| 28 | + }() |
| 29 | + |
| 30 | + init(contentView: UIView, descriptionText: String, highlightedDescriptionText: String) { |
| 31 | + self.contentView = contentView |
| 32 | + self.descriptionText = descriptionText |
| 33 | + self.highlightedDescriptionText = highlightedDescriptionText |
| 34 | + super.init(frame: .zero) |
| 35 | + addSubview(mainStackView) |
| 36 | + pinSubviewToAllEdges(mainStackView) |
| 37 | + } |
| 38 | + |
| 39 | + required init?(coder: NSCoder) { |
| 40 | + fatalError("init(coder:) has not been implemented") |
| 41 | + } |
| 42 | + |
| 43 | + private enum Appearance { |
| 44 | + |
| 45 | + static func highlightString(_ subString: String, inString: String) -> NSAttributedString { |
| 46 | + let attributedString = NSMutableAttributedString(string: inString) |
| 47 | + |
| 48 | + guard let subStringRange = inString.nsRange(of: subString) else { |
| 49 | + return attributedString |
| 50 | + } |
| 51 | + |
| 52 | + attributedString.addAttributes([.font: WPStyleGuide.fontForTextStyle(.footnote, fontWeight: .bold)], |
| 53 | + range: subStringRange) |
| 54 | + return attributedString |
| 55 | + } |
| 56 | + |
| 57 | + static let fakeAlertToDescriptionSpacing: CGFloat = 20 |
| 58 | + |
| 59 | + static let descriptionTextColor = UIColor(light: .muriel(color: .gray, .shade50), dark: .muriel(color: .gray, .shade10)) |
| 60 | + } |
| 61 | +} |
0 commit comments