Skip to content

Instantly share code, notes, and snippets.

View priore's full-sized avatar

Priore priore

View GitHub Profile
@priore
priore / Swift+HTML.swift
Last active May 5, 2018 06:51
HTML string to NSAttributedString
// Swift 4
extension String {
func toAttributedString(color: UIColor?, font: UIFont?) -> NSAttributedString {
do {
var string = self
if color != nil, font != nil {
string = """
@priore
priore / UIWebView+Height.h
Created March 31, 2017 14:21
UIWebView get height
// file: "UIWebView+Height.h"
#import <UIKit/UIKit.h>
typedef void(^WebViewContentLoaded)(NSString *html, CGSize scrollSize);
@interface UIWebView (Height) <UIWebViewDelegate>
@property (nonatomic, copy) WebViewContentLoaded webLoadedBlock;
@priore
priore / issimulator.m
Created July 20, 2016 14:13
Identifies if is running from the device or simulator
#include <sys/sysctl.h>
+ (NSString*)hardwareString
{
int name[] = {CTL_HW,HW_MACHINE};
size_t size = 100;
sysctl(name, 2, NULL, &size, NULL, 0); // getting size of answer
char *hw_machine = malloc(size);
sysctl(name, 2, hw_machine, &size, NULL, 0);
@priore
priore / BaseViewController.m
Created July 5, 2016 12:41
Tips: a solid dealloc for all classes
#import <objc/runtime.h>
@implementation BaseViewController()
- (void)dealloc
{
[CATransaction setCompletionBlock:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
@priore
priore / loadwithurl.m
Last active April 22, 2016 11:00
Standardize AFNetworking calls
// [url] : service url
// [parameters] : optional parameters
// [keyPath] : optional root key to retrieve data
// [class] : object class that should return in the array
//
// eg. [PRHTTPSessionManager loadWithURL:@"http://mioservizio.com" parameters:nil keyPath:nil class:[ProductModel class] completion:....
// eg. [PRHTTPSessionManager loadWithURL:@"http://mioservizio.com" parameters:nil keyPath:@"products.list" class:[ProductModel class] completion:....
//
+ (void)loadWithURL:(NSString*)url
@priore
priore / CustomLabel.m
Created April 18, 2016 16:24
UILabel correct way to padding left/right
// UILabel correct way to padding left/right
#import <UIKit/UIKit.h>
@interface CustomLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets edgeInsets;
@end
#import "CustomLabel.h"
@priore
priore / pep.m
Created March 24, 2016 15:32
Performance examination procedure
// performance examination procedure
#ifdef DEBUG
double then, now;
now = CFAbsoluteTimeGetCurrent();
#endif
//
// TODO: your code here
//
@priore
priore / NSObject+CRC32.m
Last active March 24, 2016 16:34
CRC32 from generic NSObject
#import <zlib.h>
- (NSUInteger)crc32
{
// the object requires NSObject-Coding https://github.com/priore/NSObject-Coding
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
return crc32(0, data.bytes, data.length);
}
@priore
priore / dispatch_cancelable_block_t.h
Created March 4, 2016 16:25
Cancellable dispatch_after
#ifndef dispatch_cancellable_block_h
#define dispatch_cancellable_block_h
#import <Foundation/Foundation.h>
// https://github.com/SebastienThiebaud/dispatch_cancelable_block/issues/2
typedef void(^dispatch_cancelable_block_t)(BOOL cancel);
NS_INLINE dispatch_cancelable_block_t dispatch_after_delay(NSTimeInterval delay, dispatch_block_t block) {
if (block == nil) {
@priore
priore / scrollviewsense.m
Created March 3, 2016 16:40
Scroll direction pulses with a value sensitivity
@interface ViewController () <UIScrollViewDelegate>
{
CGFloat initialContentOffset;
CGFloat previousContentDelta;
}
@property (nonatomic, assign) CGFloat senseValue;
@end