Utility.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Utility.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 extension UIColor {
  11. /// Returns color from its hex string
  12. ///
  13. /// - Parameter hexString: the color hex string
  14. /// - Returns: UIColor
  15. static func fromHex(hexString: String) -> UIColor {
  16. let hex = hexString.trimmingCharacters(
  17. in: CharacterSet.alphanumerics.inverted)
  18. var int = UInt32()
  19. Scanner(string: hex).scanHexInt32(&int)
  20. let a, r, g, b: UInt32
  21. switch hex.count {
  22. case 3: // RGB (12-bit)
  23. (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
  24. case 6: // RGB (24-bit)
  25. (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
  26. case 8: // ARGB (32-bit)
  27. (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
  28. default:
  29. return UIColor.clear
  30. }
  31. return UIColor(
  32. red: CGFloat(r) / 255,
  33. green: CGFloat(g) / 255,
  34. blue: CGFloat(b) / 255,
  35. alpha: CGFloat(a) / 255)
  36. }
  37. }
  38. extension UIView {
  39. // Transform a view's shape into circle
  40. func asCircle() {
  41. self.layer.cornerRadius = self.frame.width / 2;
  42. self.layer.masksToBounds = true
  43. }
  44. func setTintColor(_ color: UIColor, recursive: Bool) {
  45. tintColor = color
  46. if recursive {
  47. subviews.forEach({$0.setTintColor(color, recursive: true)})
  48. }
  49. }
  50. }
  51. extension UILabel {
  52. func sizeToFitHeight() {
  53. let tempLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: CGFloat.greatestFiniteMagnitude))
  54. tempLabel.numberOfLines = numberOfLines
  55. tempLabel.lineBreakMode = lineBreakMode
  56. tempLabel.font = font
  57. tempLabel.text = text
  58. tempLabel.sizeToFit()
  59. frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: tempLabel.frame.height)
  60. }
  61. }
  62. extension UIView
  63. {
  64. func copyView<T: UIView>() -> T {
  65. return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
  66. }
  67. func addHeight(_ height: CGFloat) {
  68. self.frame = CGRect(origin: self.frame.origin, size: CGSize(width: self.frame.width, height: self.frame.height + height))
  69. }
  70. }
  71. extension UIView.KeyframeAnimationOptions {
  72. static var curveEaseIn: UIView.KeyframeAnimationOptions {
  73. get {
  74. return UIView.KeyframeAnimationOptions(animationOptions: .curveEaseIn)
  75. }
  76. }
  77. static var curveEaseOut: UIView.KeyframeAnimationOptions {
  78. get {
  79. return UIView.KeyframeAnimationOptions(animationOptions: .curveEaseOut)
  80. }
  81. }
  82. static var curveEaseInOut: UIView.KeyframeAnimationOptions {
  83. get {
  84. return UIView.KeyframeAnimationOptions(animationOptions: .curveEaseInOut)
  85. }
  86. }
  87. static var curveLinear: UIView.KeyframeAnimationOptions {
  88. get {
  89. return UIView.KeyframeAnimationOptions(animationOptions: .curveLinear)
  90. }
  91. }
  92. init(animationOptions: UIView.AnimationOptions) {
  93. self.init(rawValue: animationOptions.rawValue)
  94. }
  95. }
  96. extension CGRect {
  97. var center: CGPoint {
  98. get {
  99. return CGPoint(x: midX, y: midY)
  100. }
  101. }
  102. }
  103. public enum MaterialKey : String{
  104. case _default
  105. }