Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
@michaelevensen
michaelevensen / PagingCollectionViewController.swift
Last active October 21, 2016 08:43
Slowly pages, not the snappy paging behavior but still nice.
import UIKit
private let reuseIdentifier = "Cell"
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
/* Return item size */
var itemSize: CGSize {
let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
return layout.itemSize
@michaelevensen
michaelevensen / PagingCollectionViewController.swift
Last active April 10, 2024 08:46
An example of perfectly paging horizontal UICollectionViewController with overflowing cells. Works great with Storyboard — no need to set any specific attributes, just add this Class to the Controller and set your desired size for the cells like you would normally.
import UIKit
private let reuseIdentifier = "Cell"
class CollectionViewController: UICollectionViewController {
/* Custom scrollView for paging */
let pagingScrollView = UIScrollView()
/* Return item size */
@michaelevensen
michaelevensen / SectionHeaderFlowLayout.swift
Created October 20, 2016 19:15
Will place section headers on top of UICollectionView.
import UIKit
class CustomSectionHeaderFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var allAttributes = super.layoutAttributesForElements(in: rect)!
// re-add the header when disappearing
var headerIsVisible = false
// Set new text with existing NSAttributedString
let titleAttributedString = NSMutableAttributedString(string: self.meetingTitles[indexPath.row], attributes: cell.meetingTitle.attributedText?.attributes(at: 0, effectiveRange: nil))
// Set new attributedText
cell.meetingTitle.attributedText = titleAttributedString
@michaelevensen
michaelevensen / NSAttributedString+MakeMutable.swift
Last active November 28, 2016 19:54
Color parts of words in attributedString. Note: Also respects any previous attributes set in Storyboard (colored words or lineHeightMultiple etc.). Also a simple function which just makes a new NSMutableString with the defined text value while keeping all existing attributes.
extension NSAttributedString {
// MARK: - Makes a copy of the existing attributes for NSAttributedString and sets color for specific string(s)
func makeStringWithColor(forString stringArray: [String], withColor color: UIColor) -> NSMutableAttributedString {
// Existing attributes
let attributedString = NSMutableAttributedString(attributedString: self)
// Iterate through strings
for specifiedString in stringArray {
@michaelevensen
michaelevensen / UIColor+Random.swift
Created October 28, 2016 11:39
Return a random color from a nice set of bright colors.
import UIKit
extension UIColor {
func random() -> UIColor {
let colors = [
UIColor(red:0.91, green:0.07, blue:0.14, alpha:1.0),
UIColor(red:0.94, green:0.23, blue:0.09, alpha:1.0),
UIColor(red:0.97, green:0.39, blue:0.05, alpha:1.0),
@michaelevensen
michaelevensen / CurrentDateFormat.swift
Last active October 28, 2016 12:27
Simple way of formatting a date.
// Date format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd. MMMM"
// Output current date in format
dateFormatter.string(from: Date())
@michaelevensen
michaelevensen / DynamicTableViewCellSize.swift
Last active October 28, 2016 18:20
Allows UITableViewCells to dynamically size based on content. Make sure to set all vertical autolayout constrains for all elements in UITableViewCell for this to work properly. Also make sure to set UILabel Lines to 0.
// Dynamically sizing cells
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 400
@michaelevensen
michaelevensen / AdjustViewSizeBasedOnKeyboard.swift
Created November 1, 2016 14:13
Adjust the view size based on whether the keyboard is open or closed. Sizes dynamically based on keyboard.
override func viewDidLoad() {
super.viewDidLoad()
// Listen for keyboard changes
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(sender: NSNotification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
// Returns class type
type(of: Element)