Skip to content

Instantly share code, notes, and snippets.

View LZhenHong's full-sized avatar
🎯
Focusing

Eden LZhenHong

🎯
Focusing
View GitHub Profile
@LZhenHong
LZhenHong / new-node-project.sh
Last active January 2, 2024 03:11
A shell script to quick setup Node project.
#!/bin/bash
path=$1
dir_name=$2
if [ ! -d "$path" ]; then
echo "Path [${path}] is not a valid directory."
exit 1
fi
@LZhenHong
LZhenHong / GitPull.py
Last active March 26, 2023 13:58
Pull all git repositories in a specific directory.
#!/usr/bin/env python3
import os
git_folder_name = ".git"
def iterateDirectory(directory, filter, action):
rootDir = os.path.normpath(directory)
for dir in os.listdir(rootDir):
path = os.path.join(rootDir, dir)
import UIKit
extension UITextField {
/// Add a trailing placeholder label that tracks the text as it changes
func addTrailingPlaceholder(_ placeholder: String) {
let label = UILabel()
label.text = placeholder
label.alpha = 0.3
label.isHidden = true
let container = UIView()
@LZhenHong
LZhenHong / Delegate.swift
Created October 18, 2021 02:31 — forked from onevcat/Delegate.swift
An auto-weak delegate for handle modern delegate pattern.
import Foundation
/// A class that keeps a weakly reference for `self` when implementing `onXXX` behaviors.
/// Instead of remembering to keep `self` as weak in a stored closure:
///
/// ```swift
/// // MyClass.swift
/// var onDone: (() -> Void)?
/// func done() {
/// onDone?()
@LZhenHong
LZhenHong / DrawNSString.m
Last active July 23, 2020 07:14
Draw NSString into UIImage.
- (UIImage *)drawString:(NSString *)drawingStr {
NSDictionary *attrs = @{
NSFontAttributeName: [UIFont systemFontOfSize:20.0],
};
CGSize size = [drawingStr boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attrs
context:nil].size;
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[drawingStr drawAtPoint:CGPointZero withAttributes:attrs];
import Foundation
// Generic Error
public struct Error: ErrorType {let reason: String}
/**
Printing version of try? Call either with standard or autoclosure approach
```
let contents = attempt{try NSFileManager.defaultManager().contentsOfDirectoryAtPath(fakePath)}
let contents = attempt{try NSFileManager.defaultManager().contentsOfDirectoryAtPath(XCPlaygroundSharedDataDirectoryURL.path!)}
@LZhenHong
LZhenHong / TestCaseSubclass.m
Created August 12, 2018 15:35 — forked from steipete/TestCaseSubclass.m
If you get an [NSProxy doesNotRecognizeSelector:_accessibilityLoadAccessibilityInformation] crash in iOS 12, here's a temporary fix for your tests. Please change the prefix before you use this! MIT licensed.
static void PSPDFFixiOS12AccessibilityTestCrash(void) {
let accessibilityLoaderClass = NSClassFromString(@"UIAccessibilityInformationLoader");
let accessibilitySEL = NSSelectorFromString(@"_loadAccessibilityInformationOnMainThread:");
__block IMP originalIMP = pspdf_swizzleSelectorWithBlock(accessibilityLoaderClass, accessibilitySEL, ^(id _self, BOOL onMainThread) {
@try {
((void (*)(id, SEL, BOOL))originalIMP)(_self, accessibilitySEL, onMainThread);
} @catch (NSException *exception) {
NSLog(@"Exception received: %@", exception);
if ([exception.name isEqualToString:NSInvalidArgumentException] && [exception.reason containsString:@"_accessibilityLoadAccessibilityInformation"]) {
NSLog(@"Ignoring IOS 12b5 weirdness...");
@LZhenHong
LZhenHong / InteractiveTransitionTableViewDeselection.m
Created August 1, 2018 08:13 — forked from smileyborg/InteractiveTransitionCollectionViewDeselection.m
Animate table view deselection alongside interactive transition on iOS 11
/*
In iOS 11, interactive view controller transitions no longer scrub by setting the layer speed to zero
and changing the timeOffset. As a result of this change, implicit animations that occur in places like
-viewWillAppear: (called during an interactive transition) no longer end up “caught in” the animation.
To get the same behavior for table view row deselection as before, you can either use UITableViewController
which implements this for you, or you can implement it manually by deselecting the row in an alongside
animation for the transition (set up in -viewWillAppear: using the transition coordinator).
Here is an example implementation which correctly handles some of the more subtle corner cases:
@LZhenHong
LZhenHong / AnyOf.swift
Created August 26, 2017 05:50 — forked from JohnSundell/AnyOf.swift
A way to easily compare a given value against an array of candidates
import Foundation
struct EquatableValueSequence<T: Equatable> {
static func ==(lhs: EquatableValueSequence<T>, rhs: T) -> Bool {
return lhs.values.contains(rhs)
}
static func ==(lhs: T, rhs: EquatableValueSequence<T>) -> Bool {
return rhs == lhs
}
@LZhenHong
LZhenHong / error-propagation.swift
Created April 28, 2017 03:30 — forked from nicklockwood/error-propagation.swift
Typed error use-case example
enum ValueError: Error {
case notFound
case typeMismatch
}
struct PropertyError: Error {
let name: String
let error: ValueError
}