Skip to content

Instantly share code, notes, and snippets.

View iAmrSalman's full-sized avatar

Amr Salman iAmrSalman

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / handleOrientationChangle.md
Created August 26, 2017 20:54
[Detect orientation change] #swift3

Here's how I got it working:

In AppDelegate.swift inside the didFinishLaunchingWithOptions function I put:

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

and then inside the AppDelegate class I put the following function:

@iAmrSalman
iAmrSalman / layoutCollectionViewCellSizing.swift
Created August 10, 2017 11:35
[collectionView cell sizing] #swift3 #collectionview #layout
func setupCellSizing() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout
}