Skip to content

Instantly share code, notes, and snippets.

View cybersamx's full-sized avatar

Samuel Chow cybersamx

  • Los Angeles, Ca
  • 10:48 (UTC -07:00)
View GitHub Profile
@cybersamx
cybersamx / UIViewSnapshot.m
Created August 6, 2014 17:39
Create a UIImageView representing a visual snapshot of another UIView object.
- (UIImageView *)createSnapshotFromView:(UIView *view) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [[UIImageView alloc] initWithImage:snapshotImage];
}
@cybersamx
cybersamx / UIStretchedButton.m
Last active August 29, 2015 14:04
Create a custom UIButton with a custom background image and stretchable width.
// 1. Create a PNG background image.
// 2. Use UIEdgeInsets to stretch the image eg. 8.0f left and right of rect to remain unstretched
// 3. Make sure the UIButton height is the same as the image height
UIImage *image = [UIImage imageNamed:@"button_background_blue.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(0.0f, 8.0f, 0.0f, 8.0f);
UIImage *stretchedImage = [image resizableImageWithCapInsets:insets];
[self.button setBackgroundImage:stretchedImage forState:UIControlStateNormal];
@cybersamx
cybersamx / CoordinatesOfSubView.m
Created August 6, 2014 17:58
To find the coordinate of subview in the view controller view coordinate system.
// Create nested subviews. self.view > subview1 > subview2
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20, 300, 300);
[self.view addSubview:subview1];
UIView *subview2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30, 200, 200);
[superview addSubview:subview2];
// To find the coordinate of subview in the view controller view coordinate system.
CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:self.view];
@cybersamx
cybersamx / NSDateFormatter_DateFormatSpecifiers.m
Last active August 29, 2015 14:04
NSDateFormatter date format specifiers
[dateFormatter setDateFormat:@"E, d M y"]; // Output: Sun, 1 5 2011
[dateFormatter setDateFormat:@"EE, dd MM yy"]; // Output: Sun, 01 05 11
[dateFormatter setDateFormat:@"EEE, ddd MMM yyy"]; // Output: Sun, 001 May 2011
[dateFormatter setDateFormat:@"EEEE, dddd MMMM yyyy"]; // Output: Sunday, 0001 May 2011
[dateFormatter setDateFormat:@"EEEEE, ddddd MMMMM yyyyy"]; // Output: S, 00001 M 2011
// Common date format specifiers
//
// y = year
// Q = quarter
@cybersamx
cybersamx / InjectAngularJSControllerToJasmine.js
Created August 6, 2014 18:22
Inject a custom AngularJS controller object to a (Jasmine) unit test spec.
// 3 ways to inject custom AngularJS controller to a Jasmine test spec.
// Method 1
describe('myNameSpace.controllers', function() {
describe('MyController', function() {
var scope, createController;
beforeEach(module('myNameSpace.controllers'));
beforeEach(inject(function($injector) {
@cybersamx
cybersamx / InjectAngularJSServiceToJasmine.js
Last active August 29, 2015 14:04
Inject a custom AngularJS service object to a (Jasmine) unit test spec.
// 2 ways to inject custom service to a Jasmine test spec.
// Method 1
describe('myNameSpace.services', function() {
describe('MyService', function() {
var createService;
beforeEach(inject(function() {
var $injector = angular.injector(['myNameSpace.services'])
@cybersamx
cybersamx / InjectAngularJSDirectiveToJasmine.js
Created August 6, 2014 18:26
Inject a custom AngularJS directive object to a (Jasmine) unit test spec.
// Simple AngularJS directive
var directives = angular.module('myNameSpace.directives', []);
directives.directive('directive', function() {
var directive = {};
directive.restrict = 'E'; // Indicates an element directive.
directive.template = 'Hello World';
return directive;
@cybersamx
cybersamx / InjectAngularJSFilterToJasmine.js
Created August 6, 2014 18:28
Inject a custom AngularJS filter object to a (Jasmine) unit test spec.
// Simple custom filer
var filters = angular.module('myNameSpace.filters', []);
filters.filter('reverse', function() {
return function(text) {
return text.split('').reverse().join('');
};
});
// 2 ways to test
@cybersamx
cybersamx / postgres_sys_commands.sql
Last active August 29, 2015 14:04
PostgreSQL System Commands
-- List of all databases
\list
-- Connect to database
\c MyDatabase
-- List of tables and relations
\dt
-- Schema of a table
@cybersamx
cybersamx / git_primer.sh
Last active August 29, 2015 14:04
Git Primer
# Find the remote origin
git remote show origin
git config --get remote.origin.url
# Describe the git repo (only works if there're tags added to the repo)
# Output: [nearest_tag_name]-[number_of_commits]-g[commit_identifier]
git describe
# List of tags
git tag # List of local tags