Skip to content

Instantly share code, notes, and snippets.

View rickytan's full-sized avatar
💭
I may be slow to respond.

Ricky Tan rickytan

💭
I may be slow to respond.
View GitHub Profile
@rickytan
rickytan / RTRouter.h
Created October 17, 2016 14:01
A app router designation
#import <Foundation/Foundation.h>
@class RTRouter;
typedef void(^RTRouteCompleteBlock)(void);
@protocol RTRoutable <NSObject>
@optional
+ (BOOL)routerDidRoutePattern:(NSString *)pattern
withParameters:(id)parameters
@rickytan
rickytan / UIImage+sRGB.m
Created September 26, 2017 15:09
convert to sRGB space
- (UIImage *)mz_convertToSRGBColorSpace
{
UIImage *newImage = self;
do {
CGImageRef CGImage = self.CGImage;
CGColorSpaceRef srcSpace = CGImageGetColorSpace(CGImage);
CGColorSpaceRef dstSpace = IS_IOS_9 ? CGColorSpaceCreateWithName(kCGColorSpaceSRGB) : CGColorSpaceCreateDeviceRGB();
// 颜色空间一样直接返回 self
if (CFEqual(srcSpace, CFAutorelease(CGColorSpaceCreateDeviceRGB())) ||
@rickytan
rickytan / MZKVOKeyPath.h
Last active September 28, 2018 07:25
Safe key path, compile time key-path checking and auto-completion
#define __mz_macro_concat(A, B) __mz_macro_concat_(A, B)
#define __mz_macro_argcount(...) __mz_macro_at(6, __VA_ARGS__, 6, 5, 4, 3, 2, 1)
#define __mz_macro_head(...) __mz_macro_head_(__VA_ARGS__, 0)
#define __mz_macro_at(N, ...) __mz_macro_concat(__mz_macro_at, N)(__VA_ARGS__)
#define __mz_macro_at0(...) __mz_macro_head(__VA_ARGS__)
#define __mz_macro_at1(_0, ...) __mz_macro_head(__VA_ARGS__)
#define __mz_macro_at2(_0, _1, ...) __mz_macro_head(__VA_ARGS__)
#define __mz_macro_at3(_0, _1, _2, ...) __mz_macro_head(__VA_ARGS__)
#define __mz_macro_at4(_0, _1, _2, _3, ...) __mz_macro_head(__VA_ARGS__)
@interface NSString (ComposedCharacter)
/**
* @description @b NSString use UTF-16 encode to store characters internally, in
* most cast, the @c length of a @b NSString is the number visible characters.
* But not for some special Characters like Emoji.
* @"😃🤡⛹🏿‍♀️⚫️🤥🤕💪🏾" this @b NSString only has 7 Emojis, but its @c length is
* 20. Sometimes we only want the human visible charater count.
*
* @code
#define RTObjectKeyPath(OBJ, PATH) (((void)(NO && ((void)(((typeof(OBJ))nil).PATH), NO)), @# PATH))
@rickytan
rickytan / UIImage+Border.m
Created January 8, 2019 11:09
UIImage Category
+ (UIImage *)rt_borderImageWithBorderColor:(UIColor *)borderColor fillColor:(UIColor *)fillColor borderWidth:(CGFloat)width radius:(CGFloat)radius forCorner:(UIRectCorner)corner
{
const CGFloat length = MAX(3, 2 * MAX(radius, width / 2) + width + 1.f / [UIScreen mainScreen].scale);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(length, length), NO, 0);
[borderColor ?: [UIColor clearColor] setStroke];
[fillColor ?: [UIColor clearColor] setFill];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(width / 2, width / 2, length - width, length - width)
byRoundingCorners:corner
cornerRadii:CGSizeMake(radius, radius)];
@rickytan
rickytan / NSString+RegExp.h
Created January 18, 2019 04:08
NSString regular expression replace
@interface NSString (RegExp)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
- (NSString *)reg_replace:(NSRegularExpression *)regexp withBlock:(NSString *(NS_NOESCAPE ^)())block;
- (NSString *)reg_replacePattern:(NSString *)pattern withBlock:(NSString *(NS_NOESCAPE ^)())block;
#pragma clang diagnostic pop
@end
@rickytan
rickytan / REGEXP.h
Last active January 31, 2019 03:50
A Macro to generate regular expression
#ifndef REGEXP
# define _regexp_stringify(x) #x
# define _regexp_stringify2(x) _regexp_stringify(x)
# define _regexp(...) ({ const char *str = _regexp_stringify2(# __VA_ARGS__); const size_t length = strlen(str); [[NSString alloc] initWithBytes:str + 1 length:length - 2 encoding:NSUTF8StringEncoding]; })
# define REGEXP(...) [NSRegularExpression regularExpressionWithPattern:_regexp(__VA_ARGS__) options:0 error:NULL]
#endif
@rickytan
rickytan / grub.cfg
Created September 4, 2021 15:15
openwrt grub turn off text console monitor
menuentry "****" {
linux /xxxxx consoleblank=5
}
@rickytan
rickytan / sample.caml
Created April 1, 2022 05:35
caml under CALayer
<?xml version="1.0" encoding="UTF-8"?>
<caml xmlns="http://www.apple.com/CoreAnimation/1.0">
<CALayer allowsEdgeAntialiasing="1" allowsGroupOpacity="1" bounds="0 0 18 18" contentsFormat="RGBA8" name="Root Layer" position="9 9" transform="">
<sublayers>
<CAShapeLayer id="#4" fillColor="1 1 1" path="0.26367188 0.7418073 m 5.57226563 6.83714 l 5.78320313 7.060576 6.1171875 7.060576 6.31933594 6.83714 c 11.62792968 0.7418073 l 11.90039068 0.4468719 11.73339848 0 11.32031248 0 c 0.57128907 0 l 0.16699219 0 0 0.4468719 0.26367188 0.7418073 c h" allowsEdgeAntialiasing="1" allowsGroupOpacity="1" bounds="0 0 11.90039068 7.060576" contentsFormat="RGBA8" name="triangle" position="8.99121096 4.030288">
<scriptComponents/>
</CAShapeLayer>
<CALayer allowsEdgeAntialiasing="1" allowsGroupOpacity="1" bounds="0 0 18 18" contentsFormat="RGBA8" name="rings" position="9 9">
<mask allowsEdgeAntialiasing="1" allowsGroupOpacity="1" backgroundColor="0 0 0" bounds="0 0 18 18" contentsFormat="RGBA8" name="mas