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 / 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!
}
@Tricertops
Tricertops / NSLiterals.m
Created May 24, 2017 06:53
Macros for creating NSArray and NSSet objects as replacement for @[…] literal.
// Macros for creating NSArray and NSSet objects as replacement for @[…] literal.
let components = NSArray(street, city, state, country);
// Advantages:
// 1. Type of NSArray is inferred, in this case it’s NSArray<NSString *> *
// 2. List of objects is type-checked, so mixing types like NSArray(@42, @"Hi") will report compilation error.
// 3. Ability to allocate any class, not just NSArray. For example NSSet and NSMutableArray.
// 4. Avoids stupid clang bracket matching bug, if you know what I mean.
@Tricertops
Tricertops / 1. Text Replacements.md
Last active December 2, 2020 23:56
All the text replacements I use on macOS and iOS.

Text Replacements

iOS: Settings ▸ General ▸ Keyboard ▸ Shortcuts
macOS: System Preferences ▸ Keyboard ▸ Text (drag them to Desktop or drag plist on the table)

Emoji

  • :) 🙂
  • :D 😄 – The most used.
  • ;) 😉
@Tricertops
Tricertops / UIColor+GuessName.m
Last active August 5, 2020 09:59
UIColor method that approximates human-readable name. Uses no special algorithm, but hard-coded 10 names for 125 colors, defined by my plain sight. No guarantee you will see them the same. Returns one of these: white, blue, purple, red, orange, yellow, green, brown, gray, black.
// The Unlicense (https://unlicense.org)
- (NSString *)guessName {
unsigned char redComponent = roundf(self.redComponent*4);
unsigned char greenComponent = roundf(self.greenComponent*4);
unsigned char blueComponent = roundf(self.blueComponent*4);
// Component methods implemented below.
static NSString * const black = @"black";
static NSString * const red = @"red";
@Tricertops
Tricertops / VDM Architecture.md
Last active August 30, 2019 05:28
How we used MVVM architecture and a reactive framework to build our latest iOS app, from theory to actual code examples.

View → Design → Model

iOS applications are usually built with MVC (Model – View – Controller) architecture, which introduces very important concept of separating actual data (Model Layer) and their presentation (View Layer), while the application logic (Controller Layer) stands between them.

View ← Controller → Model

With MVC you typically write most of the code in UIViewController, which usually represents the Controller Layer. View Layer can be easily done in Interface Builder and Model Layer usually doesn’t need a lot of code. The UIViewControleler then holds strong references to both View and Model objects and is responsible for setting them up, handling actions and listening to events.

The problem is, that this middle layer tends to hold too much code and this situation is then jokingly called Massive View Controller. When a single class sets up views, formats data values, handles user input and actions, listens for a bunch of notif

@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 / CFBinaryHeap.m
Created February 14, 2018 09:05
Implementation of NSFastEnumeration on top of CFBinaryHeap. The only way to get objects from CFBinaryHeap is to use allocated object buffer, but NSFastEnumeration doesn’t provide a way to free() it explicitly. I used autoreleased NSMutableData to manage the buffer and it somehow works.
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])providedBuffer count:(NSUInteger)providedLength {
// If this is not first invocation, finish by returning 0.
if (state->state != 0) {
return 0;
}
let count = CFBinaryHeapGetCount(self->_underlayingHeap);
// We are empty.
if (count == 0) {
return 0;
@Tricertops
Tricertops / main.m
Created December 18, 2017 10:36
NSKeyedUnarchiver cannot decode class clusters
#import <Foundation/Foundation.h>
@interface Node : NSObject <NSCoding>
@property NSString *name;
@property NSMutableArray<Node *> *targets;
@end
@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 / Appeal.md
Created May 12, 2017 16:56
Appeal to the App Review Board

I ask for repeated approval of version 4.6.1, since it was rejected for features that were previously approved in 4.6.

Version 4.6 added feature that changes app icon based on user’s location. I wrote explanation of this feature in Review Notes and this version was approved by your team. (Full text of Review Notes is embedded at the end.)

Version 4.6.1 didn’t make any changes to previously approved features, it only fixed improtant bugs and improved several UI elements. No change in app icon management was made, but this time, the app was rejected for way it approaches Alternate Icons.

This inconsistency in decisions of App Review Team is troubling for me, because I got two distinct results from submitting basically the same app. Process of reviewing new submissions should take into account previous versions of the app.
Also related, my previous submission of version 4.6.1 was rejected by asking for information which was provided in Review Notes of previous update. I don’t think that should happen. When