Skip to content

Instantly share code, notes, and snippets.

@oalansari82
oalansari82 / Extensions.swift
Created August 7, 2017 13:26
Extension for UIColor in swift 3
// Create a new file called extension and add the following code to it
extension UIColor {
static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}
@oalansari82
oalansari82 / Extensions.swift
Last active November 24, 2017 05:00
Create a better anchoring system for UI elements in Swift
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeading: CGFloat, paddingBottom: CGFloat, paddingTrailing: CGFloat, width: CGFloat, height: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
}
@oalansari82
oalansari82 / extension.swift
Created November 24, 2017 05:02
Create shortcut for RGB color and func that converts HEX to RGB
extension UIColor {
static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
static func mainColor() -> UIColor {
return UIColor.rgb(red: 105, green: 26, blue: 64)
}
static func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
@oalansari82
oalansari82 / extension.swift
Created November 24, 2017 05:03
Resize images in swift
extension UIImage {
func resizeWithPercent(percentage: CGFloat) -> UIImage? {
let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage)))
imageView.contentMode = .scaleAspectFit
imageView.image = self
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
imageView.layer.render(in: context)
guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
UIGraphicsEndImageContext()
@oalansari82
oalansari82 / extension.swift
Created November 24, 2017 05:03
Activity Indicator in Swift
extension UIActivityIndicatorView {
static func startActivityIndicator(base: UIViewController, indicator: UIActivityIndicatorView, container: UIView, loadingView: UIView) {
container.frame = base.view.frame
container.center = base.view.center
container.backgroundColor = UIColor.UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)
loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
loadingView.center = base.view.center
loadingView.backgroundColor = UIColor.UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
@oalansari82
oalansari82 / functions.php
Last active March 30, 2018 09:45
Add body class based on browser
<?php
// Only add the content below this line
add_filter('body_class','browser_body_class');
/**
*
* Add browser class based on user browser
*
*/
function browser_body_class($classes) {
class PdfTools {
func generatePdfFromCollectionView(_ collectionView: UICollectionView?, filename:String, success:(String) -> ()) {
guard let collectionView = collectionView else {
return
}
let pdfData = NSMutableData()
let contentArea = CGRect(
@oalansari82
oalansari82 / viewDidLoad.swift
Last active May 25, 2018 13:06
Replace NavigationController's title with a logo
// Here you can set View width and height as per your requirement for displaying supportView position in navigationbar
let supportView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 40))
//supportView.backgroundColor = UIColor.red
let logo = UIImageView(image: UIImage(named: "logo")) //UIImage(named: "SelectAnAlbumTitleLettering")
// customize the origin as (45,5) but can pass them as your requirement.
logo.frame = CGRect(x: 0, y: -5, width: supportView.frame.size.width, height: supportView.frame.size.height)
supportView.addSubview(logo)
supportView.contentMode = .center
@oalansari82
oalansari82 / FirebaseAuthExtention.swift
Created May 27, 2018 11:28
Firebase Auth & Storage Error Handling for iOS
// MARK: - Firebase Auth
extension Auth {
//Handle Firebase Auth Errors
static func handleFirebaseAuthErrorWith(errorCode: AuthErrorCode) -> [String] {
switch errorCode {
case .emailAlreadyInUse:
let title = "User Exist"
let message = "The email address provided already exist. Please login using your credentials."
return [title, message]
case .invalidEmail:
let attributedString = NSMutableAttributedString(string: "WhatEver text you want")
// *** Create instance of `NSMutableParagraphStyle`
let paragraphStyle = NSMutableParagraphStyle()
// *** set LineSpacing property in points ***
paragraphStyle.lineSpacing = 4 // Whatever line spacing you want in points
// *** Apply attribute to string ***
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))