PermissionsView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // PermissionsView.swift
  3. // ALCameraViewController
  4. //
  5. // Created by Alex Littlejohn on 2015/06/24.
  6. // Copyright (c) 2015 zero. All rights reserved.
  7. //
  8. import UIKit
  9. internal class PermissionsView: UIView {
  10. let iconView = UIImageView()
  11. let titleLabel = UILabel()
  12. let descriptionLabel = UILabel()
  13. let settingsButton = UIButton()
  14. let horizontalPadding: CGFloat = 50
  15. let verticalPadding: CGFloat = 50
  16. let verticalSpacing: CGFloat = 10
  17. required init?(coder aDecoder: NSCoder) {
  18. super.init(coder: aDecoder)
  19. commonInit()
  20. }
  21. override init(frame: CGRect) {
  22. super.init(frame: frame)
  23. commonInit()
  24. }
  25. func configureInView(_ view: UIView, title: String, description: String, completion: @escaping ButtonAction) {
  26. let closeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
  27. view.addSubview(self)
  28. addSubview(closeButton)
  29. titleLabel.text = title
  30. descriptionLabel.text = description
  31. closeButton.action = completion
  32. closeButton.setImage(UIImage(named: "retakeButton", in: CameraGlobals.shared.bundle, compatibleWith: nil), for: UIControl.State())
  33. closeButton.sizeToFit()
  34. let size = view.frame.size
  35. let closeSize = closeButton.frame.size
  36. let closeX = horizontalPadding
  37. let closeY = size.height - (closeSize.height + verticalPadding)
  38. closeButton.frame.origin = CGPoint(x: closeX, y: closeY)
  39. }
  40. func commonInit() {
  41. backgroundColor = UIColor(white: 0.2, alpha: 1)
  42. titleLabel.textColor = UIColor.white
  43. titleLabel.numberOfLines = 0
  44. titleLabel.textAlignment = NSTextAlignment.center
  45. titleLabel.font = UIFont(name: "AppleSDGothicNeo-Light", size: 22)
  46. titleLabel.text = localizedString("permissions.title")
  47. descriptionLabel.textColor = UIColor.lightGray
  48. descriptionLabel.numberOfLines = 0
  49. descriptionLabel.textAlignment = NSTextAlignment.center
  50. descriptionLabel.font = UIFont(name: "AppleSDGothicNeo-Regular", size: 16)
  51. descriptionLabel.text = localizedString("permissions.description")
  52. let icon = UIImage(named: "permissionsIcon", in: CameraGlobals.shared.bundle, compatibleWith: nil)!
  53. iconView.image = icon
  54. settingsButton.contentEdgeInsets = UIEdgeInsets.init(top: 6, left: 12, bottom: 6, right: 12)
  55. settingsButton.setTitle(localizedString("permissions.settings"), for: UIControl.State())
  56. settingsButton.setTitleColor(UIColor.white, for: UIControl.State())
  57. settingsButton.layer.cornerRadius = 4
  58. settingsButton.titleLabel?.font = UIFont(name: "AppleSDGothicNeo-Regular", size: 14)
  59. settingsButton.backgroundColor = UIColor(red: 52.0/255.0, green: 183.0/255.0, blue: 250.0/255.0, alpha: 1)
  60. settingsButton.addTarget(self, action: #selector(PermissionsView.openSettings), for: UIControl.Event.touchUpInside)
  61. addSubview(iconView)
  62. addSubview(titleLabel)
  63. addSubview(descriptionLabel)
  64. addSubview(settingsButton)
  65. }
  66. @objc func openSettings() {
  67. if let appSettings = URL(string: UIApplication.openSettingsURLString) {
  68. UIApplication.shared.openURL(appSettings)
  69. }
  70. }
  71. override func layoutSubviews() {
  72. super.layoutSubviews()
  73. let maxLabelWidth = frame.width - horizontalPadding * 2
  74. let iconSize = iconView.image!.size
  75. let constrainedTextSize = CGSize(width: maxLabelWidth, height: CGFloat.greatestFiniteMagnitude)
  76. let titleSize = titleLabel.sizeThatFits(constrainedTextSize)
  77. let descriptionSize = descriptionLabel.sizeThatFits(constrainedTextSize)
  78. let settingsSize = settingsButton.sizeThatFits(constrainedTextSize)
  79. let iconX = frame.width/2 - iconSize.width/2
  80. let iconY: CGFloat = frame.height/2 - (iconSize.height + verticalSpacing + verticalSpacing + titleSize.height + verticalSpacing + descriptionSize.height)/2;
  81. iconView.frame = CGRect(x: iconX, y: iconY, width: iconSize.width, height: iconSize.height)
  82. let titleX = frame.width/2 - titleSize.width/2
  83. let titleY = iconY + iconSize.height + verticalSpacing + verticalSpacing
  84. titleLabel.frame = CGRect(x: titleX, y: titleY, width: titleSize.width, height: titleSize.height)
  85. let descriptionX = frame.width/2 - descriptionSize.width/2
  86. let descriptionY = titleY + titleSize.height + verticalSpacing
  87. descriptionLabel.frame = CGRect(x: descriptionX, y: descriptionY, width: descriptionSize.width, height: descriptionSize.height)
  88. let settingsX = frame.width/2 - settingsSize.width/2
  89. let settingsY = descriptionY + descriptionSize.height + verticalSpacing
  90. settingsButton.frame = CGRect(x: settingsX, y: settingsY, width: settingsSize.width, height: settingsSize.height)
  91. }
  92. }