Skip to content

Instantly share code, notes, and snippets.

@stefanpopa
stefanpopa / UIView+Extensions.swift
Created June 27, 2019 13:48
UIView layout anchors compat
extension UIView {
var safeTopAnchor: NSLayoutYAxisAnchor {
if #available(iOS 11.0, *) {
return self.safeAreaLayoutGuide.topAnchor
}
return self.topAnchor
}
var safeLeftAnchor: NSLayoutXAxisAnchor {
@stefanpopa
stefanpopa / Buildable.swift
Created June 27, 2019 11:40
Syntactic sugar extension to build and setup an object.
import Foundation
protocol Buildable {}
extension Buildable where Self: AnyObject {
/// Syntactic sugar extension to build and setup an object.
///
/// - Parameter property: The keypath.
/// - Parameter value: The desired value to be setted at the `property` keypath.
@stefanpopa
stefanpopa / TextView.swift
Created June 27, 2019 11:38
UITextView with placeholders
import UIKit
class TextView: UITextView {
private let placeholderLabel = UILabel()
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
// Remove the padding top and left of the text view
@stefanpopa
stefanpopa / LeftAlignedCollectionViewFlowLayout.swift
Created June 27, 2019 11:35
LeftAlignedCollectionViewFlowLayout
import UIKit
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
@stefanpopa
stefanpopa / UIView+Extensions.swift
Created June 27, 2019 11:32
Extensions for UIView
import UIKit
extension UIView {
func subView<T: UIView>(type: T.Type) -> T? {
return subviews.first {
return $0 is T
} as? T
}
@stefanpopa
stefanpopa / KeyboardAwareViewController.swift
Created June 27, 2019 11:29
Keyboard aware view controller
import UIKit
open class KeyboardAwareViewController: UIViewController {
private var scrollView: UIScrollView? {
return view.subView(type: UIScrollView.self)
}
override open func viewDidLoad() {
super.viewDidLoad()
@stefanpopa
stefanpopa / Spannable.kt
Created October 2, 2017 16:39 — forked from Sirelon/Spannable.kt
Adaptive for new Kotlin Version. A simple Kotlin builder for creating SpannableStrings. Original idea from https://gist.github.com/JakeWharton/11274467.
// Copyright 2014 Robert Carr
// Copyright 2016 Alexandr Romanishin
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@stefanpopa
stefanpopa / TimeoutableLocationListener.java
Created December 15, 2015 13:27 — forked from amay077/TimeoutableLocationListener.java
[Android]TimeoutableLocationListner: TimeoutableLocationListner is implementation of LocationListener for Android. If onLocationChanged isn't called within XX mili seconds, automatically remove.
package com.amay077.android.location;
import java.util.Timer;
import java.util.TimerTask;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
# xcode-build-bump.sh
# @desc Auto-increment the build number every time the project is run.
# @usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Link Binaries With Libraries"
# 6. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)