MaterialShowcaseInstructionView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // MaterialShowcaseInstructionView.swift
  3. // MaterialShowcase
  4. //
  5. // Created by Andrei Tulai on 2017-11-16.
  6. // Copyright © 2017 Aromajoin. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. public class MaterialShowcaseInstructionView: UIView {
  11. internal static let PRIMARY_TEXT_SIZE: CGFloat = 20
  12. internal static let SECONDARY_TEXT_SIZE: CGFloat = 15
  13. internal static let PRIMARY_TEXT_COLOR = UIColor.white
  14. internal static let SECONDARY_TEXT_COLOR = UIColor.white.withAlphaComponent(0.87)
  15. internal static let PRIMARY_DEFAULT_TEXT = "Awesome action"
  16. internal static let SECONDARY_DEFAULT_TEXT = "Tap here to do some awesome thing"
  17. public var primaryLabel: UILabel!
  18. public var secondaryLabel: UILabel!
  19. // Text
  20. public var primaryText: String!
  21. public var secondaryText: String!
  22. public var primaryTextColor: UIColor!
  23. public var secondaryTextColor: UIColor!
  24. public var primaryTextSize: CGFloat!
  25. public var secondaryTextSize: CGFloat!
  26. public var primaryTextFont: UIFont?
  27. public var secondaryTextFont: UIFont?
  28. public var primaryTextAlignment: NSTextAlignment!
  29. public var secondaryTextAlignment: NSTextAlignment!
  30. public init() {
  31. // Create frame
  32. let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0)
  33. super.init(frame: frame)
  34. configure()
  35. }
  36. public required init?(coder aDecoder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. /// Initializes default view properties
  40. fileprivate func configure() {
  41. setDefaultProperties()
  42. }
  43. fileprivate func setDefaultProperties() {
  44. // Text
  45. primaryText = MaterialShowcaseInstructionView.PRIMARY_DEFAULT_TEXT
  46. secondaryText = MaterialShowcaseInstructionView.SECONDARY_DEFAULT_TEXT
  47. primaryTextColor = MaterialShowcaseInstructionView.PRIMARY_TEXT_COLOR
  48. secondaryTextColor = MaterialShowcaseInstructionView.SECONDARY_TEXT_COLOR
  49. primaryTextSize = MaterialShowcaseInstructionView.PRIMARY_TEXT_SIZE
  50. secondaryTextSize = MaterialShowcaseInstructionView.SECONDARY_TEXT_SIZE
  51. }
  52. /// Configures and adds primary label view
  53. private func addPrimaryLabel() {
  54. if primaryLabel != nil {
  55. primaryLabel.removeFromSuperview()
  56. }
  57. primaryLabel = UILabel()
  58. if let font = primaryTextFont {
  59. primaryLabel.font = font
  60. } else {
  61. primaryLabel.font = UIFont.boldSystemFont(ofSize: primaryTextSize)
  62. }
  63. primaryLabel.textColor = primaryTextColor
  64. primaryLabel.textAlignment = self.primaryTextAlignment ?? .left
  65. primaryLabel.numberOfLines = 0
  66. primaryLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
  67. primaryLabel.text = primaryText
  68. primaryLabel.frame = CGRect(x: 0,
  69. y: 0,
  70. width: getWidth(),
  71. height: 0)
  72. primaryLabel.sizeToFitHeight()
  73. addSubview(primaryLabel)
  74. }
  75. /// Configures and adds secondary label view
  76. private func addSecondaryLabel() {
  77. if secondaryLabel != nil {
  78. secondaryLabel.removeFromSuperview()
  79. }
  80. secondaryLabel = UILabel()
  81. if let font = secondaryTextFont {
  82. secondaryLabel.font = font
  83. } else {
  84. secondaryLabel.font = UIFont.systemFont(ofSize: secondaryTextSize)
  85. }
  86. secondaryLabel.textColor = secondaryTextColor
  87. secondaryLabel.textAlignment = self.secondaryTextAlignment ?? .left
  88. secondaryLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
  89. secondaryLabel.text = secondaryText
  90. secondaryLabel.numberOfLines = 0
  91. secondaryLabel.frame = CGRect(x: 0,
  92. y: primaryLabel.frame.height,
  93. width: getWidth(),
  94. height: 0)
  95. secondaryLabel.sizeToFitHeight()
  96. addSubview(secondaryLabel)
  97. frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: primaryLabel.frame.height + secondaryLabel.frame.height)
  98. }
  99. //Calculate width per device
  100. private func getWidth() -> CGFloat {
  101. //superview was left side
  102. if (self.superview?.frame.origin.x)! < CGFloat(0) {
  103. return frame.width - (frame.minX/2)
  104. } else if ((self.superview?.frame.origin.x)! + (self.superview?.frame.size.width)! >
  105. UIScreen.main.bounds.width) { //superview was right side
  106. return (frame.width - frame.minX)/2
  107. }
  108. return (frame.width - frame.minX)
  109. }
  110. /// Overrides this to add subviews. They will be drawn when calling show()
  111. public override func layoutSubviews() {
  112. super.layoutSubviews()
  113. addPrimaryLabel()
  114. addSecondaryLabel()
  115. subviews.forEach({$0.isUserInteractionEnabled = false})
  116. }
  117. }