Skip to content

Instantly share code, notes, and snippets.

View novinfard's full-sized avatar

Soheil Novinfard novinfard

View GitHub Profile
@novinfard
novinfard / environmentalVariables.swift
Created September 10, 2018 11:29
[Using Envorinmental Variables in Swift]
let dic = NSProcessInfo.processInfo().environment
if dic["MY_VARIABLE"] != nil {
// ... do secret stuff here ...
}
@novinfard
novinfard / removeUnavailableBranchedGit.sh
Last active March 26, 2020 15:23
[Remove branches which are not available in remote anymore in Git] #git #shell
git branch -v|grep \\[gone\\]|awk '{print $1}'|xargs -I{} git branch -D {}
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
@novinfard
novinfard / localFileUrl.swift
Created September 17, 2018 09:31
[Get swift local file]
if let urlpath = Bundle.main.path(forResource: "bpreg", ofType: "xml") {
let url = NSURL.fileURL(withPath: urlpath)
}
let urlpath = Bundle.main.path(forResource: "sample", ofType: "mp3")
let url = NSURL.fileURL(withPath: urlpath!)
@novinfard
novinfard / howManyInstancesExist.swift
Created September 17, 2018 14:22
[How many instance of the object exists in Swift]
class MyClass {
static var instanceCount = 0
init() {
super.init()
MyClass.instanceCount += 1
if MyClass.instanceCount > 1 {
print("MyClass.instanceCount : \(MyClass.instanceCount)")
}
}
@novinfard
novinfard / gesturesElement.swift
Last active November 28, 2018 10:59
[Adding gestures to elements]
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(labelTapped(tapGesture:)))
tapGesture.numberOfTapsRequired = 1
label?.isUserInteractionEnabled = true
label?.addGestureRecognizer(tapGesture)
// Notice: tapGesture is not reusable: create new one for each new element
@objc func labelTapped(tapGesture:UITapGestureRecognizer){
}
@novinfard
novinfard / UIPaddedLabel.swift
Created October 9, 2018 11:06
[Padded Label]
import UIKit
class UIPaddedLabel: UILabel {
var padding = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
public override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
public override var intrinsicContentSize: CGSize {
@novinfard
novinfard / UILabel+Extension.swift
Created October 9, 2018 11:08
[Set image for UILabel]
extension UILabel {
func set(image: UIImage, size: CGSize) {
let text = self.text ?? ""
let attachment = NSTextAttachment()
attachment.image = image
let verticalPosition = (font.capHeight - size.height).rounded() / 2
attachment.bounds = CGRect(x: 0, y: verticalPosition, width: size.width, height: size.height)
let attachmentStr = NSAttributedString(attachment: attachment)
let mutableAttributedString = NSMutableAttributedString()
@novinfard
novinfard / gradientBackground.swift
Created October 10, 2018 14:31
[setup gradaient background color view]
func setGradientBackground(view: UIView?) {
guard let view = view else {
return
}
let colorTop = .black
let colorBottom = .white
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
@novinfard
novinfard / transparentNavigationBar.swift
Last active October 16, 2018 14:35
[create clear background in navigation controller]
self.navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController.navigationBar.shadowImage = UIImage()
self.navigationController.navigationBar.isTranslucent = true
// self.navigationController.navigationBar.backgroundColor = UIColor.white.withAlphaComponent(0.05)
@novinfard
novinfard / flipHorizontallyImage.swift
Created October 23, 2018 14:20
[Flip horizontally and change the colour of image]
if let transformImage = myImageView.transform.rotated(by: .pi / 1) {
myImageView.image = UIImage(named: "image_name")
myImageView.transform = transformImage
}