Skip to content

Instantly share code, notes, and snippets.

View bryanjclark's full-sized avatar

Bryan Clark bryanjclark

View GitHub Profile
@ciiqr
ciiqr / zod-optional-null.ts
Last active May 8, 2024 15:11
zod optional/nullable/nullish differences
// zod schema
z.object({
// valid if string or:
optional: z.string().optional(), // field not provided, or explicitly `undefined`
nullable: z.string().nullable(), // field explicitly `null`
nullish: z.string().nullish(), // field not provided, explicitly `null`, or explicitly `undefined`
});
// type
{
@jasonsnell
jasonsnell / purpleaqijsnell.js
Last active October 7, 2020 22:42
Purple AQI Widget
I've moved this to a proper GitHub project.
Check it out at:
https://github.com/jasonsnell/PurpleAir-AQI-Scriptable-Widget
@warpling
warpling / CAMediaTimingFunction.swift
Last active June 14, 2022 11:15
Better CAMediaTimingFunctions
//
// CAMediaTimingFunction.swift
import UIKit
extension CAMediaTimingFunction {
static let linear = CAMediaTimingFunction(name: .linear)
static let easeOut = CAMediaTimingFunction(name: .easeOut)
@chriseidhof
chriseidhof / TypedNotifications.swift
Created January 26, 2015 14:41
Typed Notifications
import Foundation
class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
}
struct Notification<A> {
let name: String
}
void CRWaitMinimumDurationAndExecute(NSTimeInterval start, NSTimeInterval minimumDuration, void(^block)(void)) {
double diff = [NSDate date].timeIntervalSince1970-start;
double delayInSeconds = MAX(0.0, minimumDuration-diff);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (block) block();
});
}
//Usage
@calebd
calebd / ArrayHelpers.swift
Last active November 4, 2022 15:17
Swift Helpers
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
@jlawton
jlawton / blur-and-darken.m
Last active July 17, 2018 19:23
CIFilter Blur
// Fix for https://github.com/bryanjclark/ios-darken-image-with-cifilter
-(instancetype)darkened:(CGFloat)alpha andBlurredImage:(CGFloat)radius blendModeFilterName:(NSString *)blendModeFilterName {
CIImage *inputImage = [[CIImage alloc] initWithImage:self];
CIContext *context = [CIContext contextWithOptions:nil];
//First, create some darkness
CIFilter* blackGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
@bryanjclark
bryanjclark / Given an NSRange and a UILabel with attributed text, find the CGRect of a substring
Last active July 15, 2021 10:42
My three current contenders for solving a tricky UILabel question: given an NSRange and a UILabel that displays attributed text, can you find the rect of the text in that range? Link to my related Stack Overflow question: http://stackoverflow.com/questions/19417776/how-do-i-locate-the-cgrect-for-a-substring-of-text-in-a-uilabel
- (CGRect)rectForSubstringWithRange:(NSRange)range
{
/* Core Text methods that seem somewhere in the neighborhood of useful:
CTRunGetPositionsPtr
CTRunGetPositions
CTRunGetImageBounds
CTLineGetStringRange
CTLineGetOffsetForStringIndex
CTLineGetImageBounds
@mattt
mattt / uiappearance-selector.md
Last active March 19, 2024 12:52
A list of methods and properties conforming to `UIAppearance` as of iOS 12 Beta 3

Generate the list yourself:

$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS*.sdk/System/Library/Frameworks/UIKit.framework/Headers
$ grep UI_APPEARANCE_SELECTOR ./*     | \
  sed 's/NS_AVAILABLE_IOS(.*)//g'     | \
  sed 's/NS_DEPRECATED_IOS(.*)//g'    | \
  sed 's/API_AVAILABLE(.*)//g'        | \
  sed 's/API_UNAVAILABLE(.*)//g'      | \
 sed 's/UI_APPEARANCE_SELECTOR//g' | \
@prabirshrestha
prabirshrestha / TextFieldChanges.m
Created August 2, 2012 13:53
Reactive Cocoa examples
@synthesize firstName = _firstName;
@synthesize txtFirstName = _txtFirstName;
[RACAbleSelf(self.firstName) subscribeNext:^(id x) { [self firstNameChanged:x]; }];
[self rac_bind:RAC_KEYPATH_SELF(self.firstName) to:self.txtFirstName.rac_textSubscribable];
- (void) firstNameChanged:(id)firstName {
NSLog(@"changed: %@", firstName);
}