Last active
August 31, 2018 05:54
-
-
Save dilipiOSDeveloper/86f3cd8f04b8f938f454a170ee2e6d46 to your computer and use it in GitHub Desktop.
Important Extension Used in Swift for adding more functionality in iOS App.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extension Used for showing Country,City from lat long | |
extension UIViewController{ | |
func fetchCountryAndCity(location: CLLocation, completion: @escaping (String, String) -> ()) { | |
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in | |
if let error = error { | |
print(error) | |
} else if let country = placemarks?.first?.country, | |
let city = placemarks?.first?.locality { | |
completion(country, city) | |
} | |
} | |
} | |
} | |
Usage :- | |
let location = CLLocation(latitude: latitude, longitude: longitude) | |
fetchCountryAndCity(location: location) { country, city in | |
print("country:", country) | |
print("city:", city) | |
self.txtField.text = "\(city),\(country)" | |
} | |
*************************************************************************************************************************** | |
// Underline of UILabel | |
extension UILabel{ | |
func underlineLabel() { | |
if let textString = self.text { | |
let attributedString = NSMutableAttributedString(string: textString) | |
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: attributedString.length - 1)) | |
attributedText = attributedString | |
} | |
} | |
} | |
Usage:- | |
self.lblDealDescription.underlineLabel() | |
*************************************************************************************************************************** | |
extension UIView{ | |
func setBottomBorder() { | |
self.layer.masksToBounds = false | |
self.layer.shadowColor = UIColor.gray.cgColor | |
self.layer.shadowOffset = CGSize(width: 0.0, height: 1.0) | |
self.layer.shadowOpacity = 1.0 | |
self.layer.shadowRadius = 0.0 | |
} | |
func setRoundEdge() { | |
self.layer.borderWidth = 1.0 | |
self.layer.cornerRadius = 5.0 | |
self.layer.borderColor = UIColor.lightGray.cgColor | |
self.layer.masksToBounds = true | |
self.clipsToBounds = true | |
} | |
func viewBorder() { | |
self.layer.cornerRadius = 3.0 | |
self.layer.borderWidth = 1.0 | |
self.layer.borderColor = UIColor.lightGray.cgColor | |
self.layer.masksToBounds = true | |
} | |
} | |
*************************************************************************************************************************** | |
// Entension of Image for resizing of image before uploading to server | |
extension UIImage { | |
// First Method | |
var highestQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 1.0)! as NSData } | |
var highQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.75)! as NSData} | |
var mediumQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.5)! as NSData } | |
var lowQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.25)! as NSData} | |
var lowestQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 0.0)! as NSData } | |
// Second Method | |
func resized(withPercentage percentage: CGFloat) -> UIImage? { | |
let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage) | |
UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale) | |
defer { UIGraphicsEndImageContext() } | |
draw(in: CGRect(origin: .zero, size: canvasSize)) | |
return UIGraphicsGetImageFromCurrentImageContext() | |
} | |
// Third Method | |
func resizedTo1MB() -> UIImage? { | |
guard let imageData = UIImagePNGRepresentation(self) else { return nil } | |
var resizingImage = self | |
var imageSizeKB = Double(imageData.count) / 1000.0 // ! Or devide for 1024 if you need KB but not kB | |
while imageSizeKB > 1000 { // ! Or use 1024 if you need KB but not kB | |
guard let resizedImage = resizingImage.resized(withPercentage: 0.9), | |
let imageData = UIImagePNGRepresentation(resizedImage) | |
else { return nil } | |
resizingImage = resizedImage | |
imageSizeKB = Double(imageData.count) / 1000.0 // ! Or devide for 1024 if you need KB but not kB | |
} | |
return resizingImage | |
} | |
} | |
*************************************************************************************************************************** | |
// Date Extension | |
public extension Date { | |
public func dateString (format: String = "dd MMM, yyyy") -> String { | |
let formatter = DateFormatter() | |
formatter.dateFormat = format | |
return formatter.string(from: self) | |
} | |
} | |
*************************************************************************************************************************** | |
// TextField Extension | |
extension UITextField | |
{ | |
enum Direction | |
{ | |
case Right | |
case Left | |
} | |
func AddImage(direction:Direction,imageName:String,Frame:CGRect) | |
{ | |
let View = UIView(frame: Frame) | |
let imageView = UIImageView(frame: Frame) | |
imageView.image = UIImage(named: imageName) | |
View.addSubview(imageView) | |
if Direction.Right == direction | |
{ | |
self.rightViewMode = .always | |
self.rightView = View | |
} | |
else{ | |
self.leftViewMode = .always | |
self.leftView = View | |
} | |
} | |
func setLeftPaddingPoints(_ amount:CGFloat){ | |
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height)) | |
self.leftView = paddingView | |
self.leftViewMode = .always | |
} | |
func setRightPaddingPoints(_ amount:CGFloat) { | |
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height)) | |
self.rightView = paddingView | |
self.rightViewMode = .always | |
} | |
func textfieldBorder() { | |
self.layer.cornerRadius = 3.0 | |
self.layer.borderWidth = 1.0 | |
self.layer.borderColor = UIColor.lightGray.cgColor | |
self.layer.masksToBounds = true | |
} | |
func placeholderColor(_ color: UIColor){ | |
var placeholderText = "" | |
if self.placeholder != nil{ | |
placeholderText = self.placeholder! | |
} | |
self.attributedPlaceholder = NSAttributedString(string: placeholderText, | |
attributes: [NSForegroundColorAttributeName: UIColor.lightGray]) | |
} | |
} | |
func AddLeftImage(_ imageNamed: String){ | |
let imageView = UIImageView(frame: CGRect(x: 0, y: 1, width: self.frame.height,height: self.frame.height-2 )) | |
imageView.image = UIImage(named: imageNamed) | |
imageView.contentMode = UIViewContentMode.scaleAspectFit | |
self .setValue(UIColor.lightGray, forKeyPath: "_placeholderLabel.textColor") | |
self.leftView = imageView | |
self.leftViewMode = UITextFieldViewMode.always | |
self.addSubview(imageView) | |
} | |
func AddRightImage(_ imageNamed: String){ | |
let imageView = UIImageView(frame: CGRect(x: 0, y: 1, width: self.frame.height,height: self.frame.height-2 )) | |
imageView.image = UIImage(named: imageNamed) | |
imageView.contentMode = UIViewContentMode.scaleAspectFit | |
self .setValue(UIColor.lightGray, forKeyPath: "_placeholderLabel.textColor") | |
self.rightView = imageView | |
self.rightViewMode = UITextFieldViewMode.always | |
self.addSubview(imageView) | |
} | |
func changePlaceholderColor(color : UIColor){ | |
self .setValue(color, forKeyPath: "_placeholderLabel.textColor") | |
} | |
func useTopUnderline() { | |
let border = CALayer() | |
let borderWidth = CGFloat(1.0) | |
border.borderColor = UIColor.lightGray.cgColor | |
border.frame = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: self.frame.size.width, height: 1)) | |
border.borderWidth = borderWidth | |
self.layer.addSublayer(border) | |
self.layer.masksToBounds = true | |
} | |
func useUnderline() { | |
let border = CALayer() | |
let borderWidth = CGFloat(1.0) | |
border.borderColor = UIColor.lightGray.cgColor | |
border.frame = CGRect(origin: CGPoint(x: 0,y :self.frame.size.height - borderWidth), size: CGSize(width: self.frame.size.width, height: self.frame.size.height)) | |
border.borderWidth = borderWidth | |
self.layer.addSublayer(border) | |
self.layer.masksToBounds = true | |
} | |
*************************************************************************************************************************** | |
// Extension of Alert Controller | |
extension UIViewController { | |
func alertMessageShow(title: String, message: String) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) | |
let action = UIAlertAction(title: "OK", style: .default, handler: nil) | |
alert.addAction(action) | |
present(alert, animated: true, completion: nil) | |
} | |
} | |
*************************************************************************************************************************** | |
// Checking Internet Connection | |
extension UIViewController{ | |
func isInternetAvailable() -> Bool | |
{ | |
var zeroAddress = sockaddr_in() | |
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size) | |
zeroAddress.sin_family = sa_family_t(AF_INET) | |
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, { | |
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) { | |
SCNetworkReachabilityCreateWithAddress(nil, $0) | |
} | |
}) else { | |
return false | |
} | |
var flags: SCNetworkReachabilityFlags = [] | |
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { | |
return false | |
} | |
let isReachable = flags.contains(.reachable) | |
let needsConnection = flags.contains(.connectionRequired) | |
return (isReachable && !needsConnection) | |
} | |
} | |
Usage:- | |
import SystemConfiguration // import | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if (!self.isInternetAvailable()){ | |
// Internet connection not available | |
self.alertMessageShow(title: "No Internet Connection", message: "Make sure your device is connected to the internet.") | |
} | |
else{ | |
//Internet connection available | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment