Skip to content

Instantly share code, notes, and snippets.

View Tricertops's full-sized avatar
:octocat:

Martin Kiss Tricertops

:octocat:
View GitHub Profile
@Tricertops
Tricertops / UIImage+MostUsedColor.m
Last active January 26, 2019 05:06
UIImage method that makes color histogram from the image and sort them by number of occurences. Clone and edit for your specific purpose.
- (NSMutableData *)mutableRGBAData {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGSize scaledSize = (CGSize){
.width = self.size.width * self.scale,
.height = self.size.height * self.scale,
};
NSUInteger const bytesPerPixel = 4;
NSUInteger const bitsPerComponent = 8;
@Tricertops
Tricertops / UITableViewDataSource.m
Created September 28, 2013 16:57
Animated scrolling for table view section index.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSUInteger section = ...
CGRect rect = [self.tableView rectForSection:section];
// Include content inset
rect.origin.y -= self.tableView.contentInset.top;
// Restore previous offset and animate to destination
CGPoint offset = self.tableView.contentOffset;
@Tricertops
Tricertops / KVObserver.h
Last active December 24, 2015 22:49
Simplest KVO using blocks. You don't have to use ReactiveCocoa to blockify KVO.
#import <Foundation/Foundation.h>
typedef void(^KVObservingBlock)(id value, NSDictionary *change);
@interface KVObserver : NSObject
- (instancetype)initWithBlock:(KVObservingBlock)block;
@end
@Tricertops
Tricertops / gist:9738484
Created March 24, 2014 11:23
Runtime @protocol introspection
+ (void)enumerateAdoptedProtocolsOfProtocol:(Protocol *)protocol usingBlock:(void(^)(Protocol *adoptedProtocol))block {
unsigned int adoptedCount = 0;
Protocol * __unsafe_unretained *adoptedProtocolList = protocol_copyProtocolList(protocol, &adoptedCount);
if (adoptedProtocolList) {
for (NSUInteger index = 0; index < adoptedCount; index++) {
Protocol *adoptedProtocol = adoptedProtocolList[index];
block(adoptedProtocol);
}
free(adoptedProtocolList);
@Tricertops
Tricertops / Gloom.dvtcolortheme
Last active August 29, 2015 13:59
Gloom: My current Xcode colour theme derived from bundled Dusk
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DVTConsoleDebuggerInputTextColor</key>
<string>0.715709 0.129913 0.185409 1</string>
<key>DVTConsoleDebuggerInputTextFont</key>
<string>Menlo-Regular - 13.0</string>
<key>DVTConsoleDebuggerOutputTextColor</key>
<string>1 1 1 1</string>
@Tricertops
Tricertops / Request.m
Created April 24, 2014 17:41
NSError Recovery Attempting using Objective-Chain. Idea is, that the class, where the error occured knows how to recover from it. I'm using standard NSError recovery mechanism, which is not used in UIKit (but is used in AppKit), where the error instance holds referece to a callback object. Objective-Chain provides a reactive implementation of su…
// Network request failed, due to lost internet conenction...
OCAErrorRecoveryAttempter *recoveryAttempter = [[OCAErrorRecoveryAttempter alloc] init];
// Recovery suggestion will be be presented together with error message.
recoveryAttempter.recoverySuggestion = @"Make sure you are connected to the Internet and try again.";
// Creates reactive recovery option and chains an invocation to [self start].
[[recoveryAttempter addRecoveryOptionWithTitle:@"Retry"] invoke:OCAInvocation(self, start)];
@Tricertops
Tricertops / KVOCollection-Example.m
Last active August 29, 2015 14:01
Maybe the simplest way to implement Key-Value Observing compliant collection.
// This triggers correct KVO insertion notification:
[parent.mutableChildren addObject:@"Child"];
@Tricertops
Tricertops / emoji-clock.m
Last active September 2, 2015 11:27
Objective-C method that returns Emoji symbol of a clock for any given hour.
- (NSString *)clockForHour:(NSUInteger)hour {
NSArray *clocks = @[ @"🕛", @"🕐", @"🕑", @"🕒", @"🕓", @"🕔",
@"🕕", @"🕖", @"🕗", @"🕘", @"🕙", @"🕚" ];
NSUInteger index = hour % clocks.count;
return [clocks objectAtIndex:index];
}
@Tricertops
Tricertops / ThreadSafeProxy.h
Last active July 16, 2017 16:03
Simple and effective thread-safe proxy that forwards all method invocations inside a locked scope. Definitely not the fastest thread-safe implementation ever.
@import Foundation;
@interface NSObject (ThreadSafeProxy)
- (instancetype)threadSafe;
@end
@Tricertops
Tricertops / UIFont+Features.swift
Last active June 14, 2021 08:21
UIFont extension to derive new fonts by adding CoreText Font Features, like Proportional Number or Alternate Punctuation.
import UIKit
import CoreText
extension UIFont {
typealias Feature = (type: Int, selector: Int)
struct Features {
static var ProportionalNumbers: Feature = (kNumberSpacingType, kProportionalNumbersSelector)
static var AlternatePunctuation: Feature = (kCharacterAlternativesType, 1) // Magic!
}