Extension.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Extension.swift
  3. // Product Calculator
  4. //
  5. // Created by Suraj Kumar Mandal on 10/11/21.
  6. //
  7. import UIKit
  8. import Foundation
  9. extension UIView{
  10. func cardView() -> Void {
  11. self.layer.cornerRadius = 10
  12. self.layer.shadowColor = UIColor.gray.cgColor
  13. self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
  14. self.layer.shadowRadius = 6.0
  15. self.layer.shadowOpacity = 0.5
  16. }
  17. }
  18. extension UITextField {
  19. func datePicker<T>(target: T,
  20. doneAction: Selector,
  21. cancelAction: Selector,
  22. datePickerMode: UIDatePicker.Mode = .date) {
  23. let screenWidth = UIScreen.main.bounds.width
  24. func buttonItem(withSystemItemStyle style: UIBarButtonItem.SystemItem) -> UIBarButtonItem {
  25. let buttonTarget = style == .flexibleSpace ? nil : target
  26. let action: Selector? = {
  27. switch style {
  28. case .cancel:
  29. return cancelAction
  30. case .done:
  31. return doneAction
  32. default:
  33. return nil
  34. }
  35. }()
  36. let barButtonItem = UIBarButtonItem(barButtonSystemItem: style,
  37. target: buttonTarget,
  38. action: action)
  39. return barButtonItem
  40. }
  41. let datePicker = UIDatePicker(frame: CGRect(x: 0,
  42. y: 0,
  43. width: screenWidth,
  44. height: 216))
  45. datePicker.datePickerMode = datePickerMode
  46. //datePicker.maximumDate = Date()
  47. // iOS 14 and above
  48. if #available(iOS 14, *) {
  49. datePicker.preferredDatePickerStyle = .wheels
  50. datePicker.sizeToFit()
  51. }
  52. self.inputView = datePicker
  53. let toolBar = UIToolbar(frame: CGRect(x: 0,
  54. y: 0,
  55. width: screenWidth,
  56. height: 44))
  57. toolBar.setItems([buttonItem(withSystemItemStyle: .cancel),
  58. buttonItem(withSystemItemStyle: .flexibleSpace),
  59. buttonItem(withSystemItemStyle: .done)],
  60. animated: true)
  61. self.inputAccessoryView = toolBar
  62. }
  63. }
  64. extension UIWindow {
  65. static var key: UIWindow? {
  66. if #available(iOS 13, *) {
  67. return UIApplication.shared.windows.first { $0.isKeyWindow }
  68. } else {
  69. return UIApplication.shared.keyWindow
  70. }
  71. }
  72. }
  73. extension UIViewController {
  74. func convertDateFormat(inputDate: String) -> String {
  75. let olDateFormatter = DateFormatter()
  76. olDateFormatter.dateFormat = "yyyy-MM-dd"
  77. let oldDate = olDateFormatter.date(from: inputDate)
  78. let convertDateFormatter = DateFormatter()
  79. convertDateFormatter.dateFormat = "dd/MM/yyyy"
  80. return convertDateFormatter.string(from: oldDate!)
  81. }
  82. }
  83. extension Date {
  84. init(dateString:String) {
  85. self = Date.iso8601Formatter.date(from: dateString)!
  86. }
  87. static let iso8601Formatter: ISO8601DateFormatter = {
  88. let formatter = ISO8601DateFormatter()
  89. formatter.formatOptions = [.withFullDate,
  90. .withTime,
  91. .withDashSeparatorInDate,
  92. .withColonSeparatorInTime]
  93. return formatter
  94. }()
  95. }
  96. extension String {
  97. func stringBefore(_ delimiter: Character) -> String {
  98. if let index = firstIndex(of: delimiter) {
  99. return String(prefix(upTo: index))
  100. } else {
  101. return ""
  102. }
  103. }
  104. func stringAfter(_ delimiter: Character) -> String {
  105. if let index = firstIndex(of: delimiter) {
  106. return String(suffix(from: index).dropFirst())
  107. } else {
  108. return ""
  109. }
  110. }
  111. }