Extension file
Fri Sep 08 2023 05:23:34 GMT+0000 (Coordinated Universal Time)
Saved by @hasnat #ios #swift #file #extension
// // File.swift // Service Provider // // Created by Mac HD on 13 Sep, 2023. // import Foundation import UIKit import ObjectiveC extension Encodable { func toDictionary() -> [String: Any] { let mirror = Mirror(reflecting: self) var dictionary = [String: Any]() for child in mirror.children { guard let label = child.label else { continue } dictionary[label] = child.value } return dictionary } } extension UIButton { private struct AssociatedKeys { static var idKey = "idKey" } var id: String? { get { return objc_getAssociatedObject(self, &AssociatedKeys.idKey) as? String } set { objc_setAssociatedObject(self, &AssociatedKeys.idKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } } extension UIViewController { func nextVC(identifier: String) { var storyboard : UIStoryboard if AppDelegate.selectedLanguage == "en" { storyboard = UIStoryboard(name: "Main", bundle: nil) } else { storyboard = UIStoryboard(name: "arabic", bundle: nil) } let vc = storyboard.instantiateViewController(withIdentifier: identifier) vc.modalPresentationStyle = .fullScreen self.present(vc, animated: false, completion: nil) } // MARK: - Toast func showToast(message : String) { let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) alert.view.backgroundColor = UIColor.lightGray alert.view.alpha = 0.3 alert.view.tintColor = .blue alert.view.tintColor = hexStringToUIColor(hex: "FEDA22") alert.view.layer.cornerRadius = alert.view.layer.frame.height/3 self.present(alert, animated: true) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) { alert.dismiss(animated: true) } } func setToast(message : String, seconds: Double = 1.0) { let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) alert.view.backgroundColor = UIColor.white alert.view.alpha = 0.3 alert.view.tintColor = .blue alert.view.tintColor = hexStringToUIColor(hex: "FEDA22") alert.view.layer.cornerRadius = alert.view.layer.frame.height/3 self.present(alert, animated: true) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds) { alert.dismiss(animated: true) } } // MARK: - Color func hexStringToUIColor (hex:String) -> UIColor { var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) { cString.remove(at: cString.startIndex) } if ((cString.count) != 6) { return UIColor.gray } var rgbValue:UInt64 = 0 Scanner(string: cString).scanHexInt64(&rgbValue) return UIColor( red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, blue: CGFloat(rgbValue & 0x0000FF) / 255.0, alpha: CGFloat(1.0) ) } // MARK: - DateTime func getCurrentTimeStamp() -> String { let time = Double(NSDate().timeIntervalSince1970) let timeStamp = String(Int(time * 1000)) return timeStamp } func getDateTime(timeStamp : String, format: String) -> String { var t1 = Int(timeStamp) ?? 1 t1 = t1/1000 let date = NSDate(timeIntervalSince1970: TimeInterval(t1)) let dateFormatter = DateFormatter() dateFormatter.dateFormat = format let dateString = dateFormatter.string(from: date as Date) print("Date = \(dateString)") return dateString } func getDateTimeInArabic(timestamp: TimeInterval, format: String) -> String? { // Create a DateFormatter instance with Arabic locale settings let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "ar") dateFormatter.dateFormat = "dd MMM, yyyy HH:mm a" // Convert the timestamp to a Date object let date = Date(timeIntervalSince1970: timestamp) // Format the Date as an Arabic datetime string let arabicDateTime = dateFormatter.string(from: date) return arabicDateTime } func getDifference(recent: Date, previous: Date) -> (month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?) { let day = Calendar.current.dateComponents([.day], from: previous, to: recent).day let month = Calendar.current.dateComponents([.month], from: previous, to: recent).month let hour = Calendar.current.dateComponents([.hour], from: previous, to: recent).hour let minute = Calendar.current.dateComponents([.minute], from: previous, to: recent).minute let second = Calendar.current.dateComponents([.second], from: previous, to: recent).second return (month: month, day: day, hour: hour, minute: minute, second: second) } // MARK: - Validation func isValidName(testName:String) -> Bool { guard testName.count > 2, testName.count < 16 else { return false } let predicateTest = NSPredicate(format: "SELF MATCHES %@", "^(([^ ]?)(^[a-zA-Z].*[a-zA-Z]$)([^ ]?))$") return predicateTest.evaluate(with: testName) } func isValidEmail(_ email: String) -> Bool { let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx) return emailPred.evaluate(with: email) } func isValidPhone(_ mobilenumber: String) -> Bool { // optional plus sign let phoneRegex = "^[0-9+]{0,1}+[0-9]{5,16}$" let phoneTest = NSPredicate(format: "SELF MATCHES %@", phoneRegex) return phoneTest.evaluate(with: mobilenumber) } func isValidPhoneNumber(_ phoneNumber: String) -> Bool { // must add plus sign let regEx = "^\\+(?:[0-9]?){6,14}[0-9]$" let phoneCheck = NSPredicate(format: "SELF MATCHES[c] %@", regEx) return phoneCheck.evaluate(with: phoneNumber) } func isValidPassword() { } } // MARK: - UIviews extension UIView { func addshadow(top: Bool, left: Bool, bottom: Bool, right: Bool, shadowRadius: CGFloat) { // self.backgroundColor = UIColor.white self.layer.masksToBounds = false self.layer.cornerRadius = 8 self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0) self.layer.shadowRadius = shadowRadius self.layer.shadowOpacity = 0.6 self.layer.shadowColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0).cgColor let path = UIBezierPath() var x: CGFloat = 0 var y: CGFloat = 0 var viewWidth = self.frame.width var viewHeight = self.frame.height // here x, y, viewWidth, and viewHeight can be changed in // order to play around with the shadow paths. if (!top) { y+=(shadowRadius+1) } if (!bottom) { viewHeight-=(shadowRadius+1) } if (!left) { x+=(shadowRadius+1) } if (!right) { viewWidth-=(shadowRadius+1) } // selecting top most point path.move(to: CGPoint(x: x, y: y)) path.addLine(to: CGPoint(x: x, y: viewHeight)) path.addLine(to: CGPoint(x: viewWidth, y: viewHeight)) path.addLine(to: CGPoint(x: viewWidth, y: y)) path.close() self.layer.shadowPath = path.cgPath } func roundCorners(corners: UIRectCorner, radius: CGFloat) { let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.cgPath layer.mask = mask } } // MARK: - Image extension UIImageView { func setImageColor(color: UIColor) { let templateImage = self.image?.withRenderingMode(.alwaysTemplate) self.image = templateImage self.tintColor = color } }
Comments