Skip to content

Instantly share code, notes, and snippets.

View alex-zige's full-sized avatar
🤞
Focusing

Alex Z. Li alex-zige

🤞
Focusing
View GitHub Profile
@alex-zige
alex-zige / gist:8985562
Created February 13, 2014 22:49
iOS font control
myLabel.textColor = [UIColor whiteColor];
myLabel.font = [UIFont systemFontOfSize:14];
myLabel.textAlignment = NSTextAlignmentCenter;
myLabel.adjustsFontSizeToFitWidth = YES;
@alex-zige
alex-zige / gist:8948097
Created February 12, 2014 01:19
iOS read local JSON for object parsing
NSString *jsonFileName =@"your-json-file-name";
NSString *filePath = [[NSBundle mainBundle] pathForResource:jsonFileName ofType:@"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
@alex-zige
alex-zige / gist:8411571
Created January 14, 2014 01:39
Keychain Security Value Data is return CFDataRef, to cast as NSString you have to parse it!
NSData *passData = [keychain objectForKey:(id)kSecValueData];
NSString *pass = [[NSString alloc] initWithBytes:[passData bytes] length:[passData length] encoding:NSUTF8StringEncoding];
@alex-zige
alex-zige / gist:8294060
Created January 7, 2014 03:04
Shell script for one-command bootstrap rails 4 project with ruby 2.0.0. [4.0.2] latest version. Please put the code into your ~/.bash_profile and call bootstrap_rails4 in terminal inside your target folder
function set_ruby2_with_gemset(){
local result=${PWD##*/}
local result=${result// /_}
local result=$(echo $result | tr '[A-Z]' '[a-z]')
echo "prepare ruby 2.0 with gemset "$result
rvm --create ruby-2.0.0-p247@$result
}
function use_ruby2_with_gemset(){
local result=${PWD##*/}
@alex-zige
alex-zige / gist:8293850
Created January 7, 2014 02:37
A quick shell script for setting up gemset with ruby2. auto-mapping gemset name to be lowercase and underscore version of your current folder name.
function set_ruby2_with_gemset(){
local result=${PWD##*/}
local result=${result// /_}
local result=$(echo $result | tr '[A-Z]' '[a-z]')
echo "prepare ruby 2.0 with gemset "$result
rvm --create ruby-2.0.0-p247@$result
}
function use_ruby2_with_gemset(){
@alex-zige
alex-zige / gist:8216563
Last active January 1, 2016 23:29
Rails standard time to object-c NSdate or vice versa
//Rails time to NSdate
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZ"];
NSDate *date = [df dateFromString:@"2013-04-07T21:19:14Z"];
//NSDate to String
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm a"];
/*
File: KeychainItemWrapper.h
Abstract:
Objective-C wrapper for accessing a single keychain item.
Version: 1.2 - ARCified
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
@alex-zige
alex-zige / gist:8177726
Created December 30, 2013 04:16
custom background image for IOS UIRefreshControl
-(void)preparePulltoRefresh{
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor whiteColor];
[refreshControl addTarget:self action:@selector(reloadMeetings) forControlEvents:UIControlEventValueChanged];
//creating view for extending background color
CGRect frame = self.tableView.bounds;
frame.origin.y = -frame.size.height;
UIView *refreshBackgroundView = [[UIView alloc]initWithFrame:frame];
refreshBackgroundView.backgroundColor = UIColorFromRGB(0xXXXXXX);
@alex-zige
alex-zige / gist:7244178
Created October 31, 2013 03:57
GDC dispatch example
-(void)viewDidLayoutSubviews
{ [super viewDidLayoutSubviews];
dispatch_queue_t imageReader = dispatch_queue_create("Image Reader", NULL);
dispatch_async(imageReader, ^{
[NSThread sleepForTimeInterval:1];
UIImage *image = [UIImage imageNamed:self.imageName]; // may take time to load
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
self.imageView.contentMode = UIViewContentModeTopLeft;
@alex-zige
alex-zige / gist:6981762
Last active December 25, 2015 13:09
Cookie based device stragtegy
#devise/cookie_token_auth_strategy
module CookieTokenAuthStrategy
Warden::Strategies.add(:token_cookie_strategy) do
def valid?
token_from_cookie
end
def authenticate!
if token_from_cookie.present? && (user = User.find_for_token_authentication(:auth_token => token_from_cookie)).present?
delete_token_from_cookie