Skip to content

Instantly share code, notes, and snippets.

@benpackard
benpackard / Fetching MLB Scores for isPizzaHalfPrice
Created May 6, 2022
This PHP script is supposed to scrape MLB scores from ESPN but stopped working recently.
View Fetching MLB Scores for isPizzaHalfPrice
<?php
class Game {
public $espn_id, $date, $home_team, $visiting_team, $home_score, $visiting_score, $is_preseason;
}
$urls = build_URLs();
$games = array();
foreach ($urls as $url) {
$games = array_merge($games, get_games($url));
@benpackard
benpackard / EnumeratedSegmentedControl.swift
Created Nov 13, 2018
Set and get selected enum case from a `UISegmentedControl` without relying on indexes or strings.
View EnumeratedSegmentedControl.swift
import UIKit
protocol Segmentable: RawRepresentable & CaseIterable { }
class EnumeratedSegmentedControl<T: Segmentable>: UISegmentedControl where T.RawValue == String {
convenience init() {
let items = T.allCases.map { $0.rawValue }
self.init(items: items)
}
@benpackard
benpackard / LargeTitleAdjustingNavigationController.swift
Last active Nov 10, 2018
A `UINavigationController` that adjusts the font size of its large title labels to fit its content
View LargeTitleAdjustingNavigationController.swift
import UIKit
/// A `UINavigationController` that adjusts the font size of its large title labels to fit its content
class TitleSizeAdjustingNavigationController: UINavigationController {
var minimumScaleFactor: CGFloat = 0.5
override func viewDidLayoutSubviews() {
guard navigationBar.prefersLargeTitles else { return }
updateLargeTitleLabels()
@benpackard
benpackard / SelfSizingTableHeaderAndTableFooterViews.swift
Last active Dec 18, 2017 — forked from smileyborg/SelfSizingTableHeaderAndTableFooterViews.swift
How to manually self-size UITableView tableHeaderView/tableFooterView in iOS 11
View SelfSizingTableHeaderAndTableFooterViews.swift
// For the best results, your tableHeaderView/tableFooterView should be a UITableViewHeaderFooterView with your content inside the contentView.
let tableHeaderView = UITableViewHeaderFooterView()
let fittingSize = CGSize(width: tableView.bounds.width - (tableView.safeAreaInsets.left + tableView.safeAreaInsets.right), height: 0)
let size = tableHeaderView.systemLayoutSizeFitting(fittingSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
tableHeaderView.frame = CGRect(origin: .zero, size: size)
tableView.tableHeaderView = tableHeaderView
// In particular, note that we call systemLayoutSizeFitting on the contentView of the UITableViewHeaderFooterView.
// When you set this view to the tableHeaderView/tableFooterView on the table view, the table view will preserve the existing size of its frame.
@benpackard
benpackard / UIViewController+InputAccessory.swift
Last active Nov 3, 2017
UIViewController extension to add Previous, Next, and Done buttons to a group of UITextFields
View UIViewController+InputAccessory.swift
extension UIViewController {
func addInputAccessoryForTextFields(textFields: [UITextField], dismissable: Bool = true, previousNextable: Bool = false) {
for (index, textField) in textFields.enumerate() {
let toolbar: UIToolbar = UIToolbar()
toolbar.sizeToFit()
var items = [UIBarButtonItem]()
if previousNextable {
let previousButton = UIBarButtonItem(image: UIImage(named: "Backward Arrow"), style: .Plain, target: nil, action: nil)
previousButton.width = 30
@benpackard
benpackard / UIViewController+InputAccessory.swift
Last active May 10, 2016
UIViewController extension to add Done button to a UITextField
View UIViewController+InputAccessory.swift
extension UIViewController {
func addDismissingInputAccessoryToTextField(textField: UITextField) {
let doneButton = UIBarButtonItem(barButtonSystemItem: .Done, target: view, action: #selector(UIView.endEditing))
let toolbar: UIToolbar = UIToolbar()
toolbar.sizeToFit()
let spacer = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
toolbar.setItems([spacer, doneButton], animated: false)
textField.inputAccessoryView = toolbar
}
}
@benpackard
benpackard / Feedable.swift
Created Mar 31, 2016
Feedable protocol for Array of Photos
View Feedable.swift
import Foundation
class Photo {
let title: String
init(title: String) {
self.title = title
}
}