Skip to content

Instantly share code, notes, and snippets.

View iAmrSalman's full-sized avatar

Amr Salman iAmrSalman

View GitHub Profile
@iAmrSalman
iAmrSalman / ThemeManager.swift
Created September 2, 2017 10:50
[Theme Manager] #theme #swift3 #apperance
import UIKit
enum Theme: Int {
case dark, light
var dark: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("212121")
@iAmrSalman
iAmrSalman / orintation.md
Created September 6, 2017 09:38
[Orintation] #swift3

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers.

This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews or relying on inheritance.

Swift 3

In AppDelegate:

/// set orientations you want to be allowed in this property by default
@iAmrSalman
iAmrSalman / UIImageExtention.swift
Created September 6, 2017 09:45
[Image from view] #swift3 #uiimage #uiview
extension UIImage{
convenience init(view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(cgImage: (image?.cgImage)!)
}
@iAmrSalman
iAmrSalman / StringSize.md
Created September 9, 2017 12:35
[StringSize] #swift3 #string

Use an extension on String

Swift 3

extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
@iAmrSalman
iAmrSalman / changes.bash
Created September 17, 2017 15:31
[How to count total lines changed by a specific author in a Git repository?] #git
git log --shortstat --pretty="%cE" | sed 's/\(.*\)@.*/\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\1 0 insertions\(+\), \2/;s/\(\+\)$/\(\+\), 0 deletions\(-\)/;s/insertions?\(\+\), //;s/ deletions?\(-\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'
@iAmrSalman
iAmrSalman / sharing.swift
Created September 22, 2017 22:12
[sharing] #swift
func share(message: String, image: UIImage, completionHandler: (() -> Void)? = nil) {
let activityViewController = UIActivityViewController(activityItems: [message, image], applicationActivities: nil)
if let completionHandler = completionHandler {
activityViewController.completionWithItemsHandler = { _ in
completionHandler()
}
}
self.present(activityViewController, animated: true, completion: nil)
}
@iAmrSalman
iAmrSalman / progressHUD.swift
Created September 27, 2017 11:33
[loading indicator] #swift3 #customComponant
import UIKit
class ProgressHUD: UIVisualEffectView {
var text: String? {
didSet {
label.text = text
}
}
@iAmrSalman
iAmrSalman / playaudio.swift
Created October 8, 2017 09:52
[play audio] #swift4 #avfoundation
import AVFoundation
var player: AVAudioPlayer?
func playSound() {
guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
@iAmrSalman
iAmrSalman / BundleExtension.swift
Created November 8, 2017 08:52
[App Version & Build number] get the current build information from info.plist #swift3 #info.plist
extension Bundle {
var releaseVersionNumber: String? {
return infoDictionary?["CFBundleShortVersionString"] as? String
}
var buildVersionNumber: String? {
return infoDictionary?["CFBundleVersion"] as? String
}
}
Bundle.main.releaseVersionNumber
@iAmrSalman
iAmrSalman / gitignoreAfterPush.md
Created November 8, 2017 14:03
[remove ignored files after push] #git

The series of commands below will remove all of the items from the Git Index (not from the working directory or local repo), and then updates the Git Index, while respecting git ignores. PS. Index = Cache

First:

git rm -r --cached . 
git add . 

Then: