SignedOutView.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import UIKit
  15. /// This view is shown on the `User` screen when no one in signed in.
  16. final class SignedOutView: UIView {
  17. init() {
  18. super.init(frame: CGRect.zero)
  19. setupSubviews()
  20. }
  21. @available(*, unavailable)
  22. required init?(coder: NSCoder) {
  23. fatalError("init(coder:) has not been implemented")
  24. }
  25. /// Layout exclamation symbol and label explaining there is no user signed in
  26. private func setupSubviews() {
  27. let systemImageName = "exclamationmark.circle"
  28. let placeHolderImage = UIImage(systemName: systemImageName)?
  29. .withTintColor(.secondaryLabel, renderingMode: .alwaysOriginal)
  30. let imageView = UIImageView(image: placeHolderImage)
  31. imageView.translatesAutoresizingMaskIntoConstraints = false
  32. addSubview(imageView)
  33. NSLayoutConstraint.activate([
  34. imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
  35. imageView.centerYAnchor.constraint(equalTo: centerYAnchor, constant: -50),
  36. imageView.heightAnchor.constraint(equalToConstant: 100),
  37. imageView.widthAnchor.constraint(equalToConstant: 100),
  38. ])
  39. let label = UILabel()
  40. label.numberOfLines = 3
  41. label.attributedText = configuredAttributedString()
  42. addSubview(label)
  43. label.translatesAutoresizingMaskIntoConstraints = false
  44. label.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 20).isActive = true
  45. label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  46. label.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.80).isActive = true
  47. }
  48. private func configuredAttributedString() -> NSAttributedString {
  49. let paragraph = NSMutableParagraphStyle()
  50. paragraph.lineBreakMode = .byWordWrapping
  51. paragraph.alignment = .center
  52. let attributes: [NSAttributedString.Key: Any] = [
  53. .paragraphStyle: paragraph,
  54. .foregroundColor: UIColor.secondaryLabel,
  55. ]
  56. let imageAttachment = NSTextAttachment()
  57. let imageName = "person.crop.circle.fill.badge.plus"
  58. let image = UIImage(systemName: imageName)?
  59. .withTintColor(.systemOrange, renderingMode: .alwaysOriginal)
  60. imageAttachment.image = image
  61. let firstPartOfString = "There are no users currently signed in. Press the "
  62. let secondPartOfString = " button on the tab bar and select a login option."
  63. let fullString = NSMutableAttributedString(string: firstPartOfString)
  64. fullString.append(NSAttributedString(attachment: imageAttachment))
  65. fullString.append(NSAttributedString(string: secondPartOfString))
  66. fullString.addAttributes(attributes, range: NSRange(location: 0, length: fullString.length))
  67. return fullString
  68. }
  69. }