Skip to content

Instantly share code, notes, and snippets.

@situee
situee / objc_singleton.m
Last active August 29, 2015 14:04
objective-c singleton
// GCD using "instancetype" as return type
+ (instancetype)shared
{
static id shared;
static dispatch_once_t token;
dispatch_once(&token, ^{
shared = [[self alloc] init];
});
return shared;
}
@situee
situee / ios_create_rootViewController.m
Created July 11, 2014 05:39
You would use code something like this to programmatically create a window, install the root view controller, and make the window visible:
// https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html#//apple_ref/doc/uid/TP40012555-CH4-SW1
// Important: When you create a window programmatically, always set the size of the window to the full bounds of the screen object.
// In particular, don’t reduce the size of the window to accommodate the status bar or any other items.
// If you are using view controllers, the view controller should handle the sizing of your views automatically.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myViewController = [[MyViewController alloc] init];
window.rootViewController = myViewController;
[window makeKeyAndVisible];
@situee
situee / gist:9895025
Created March 31, 2014 15:32
set topLayoutGuide programmatically; auto layout
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = myViewController.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
[myViewController.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]"
options: 0
metrics: nil
views: viewsDictionary]
];
self.view.layoutSubviews; // You must call this method here or the system raises an exception
public static int getTextHeight(String text, int maxWidth, float textSize, Typeface typeface) {
TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
paint.setTextSize(textSize);
paint.setTypeface(typeface);
int lineCount = 0;
int index = 0;
int length = text.length();
//viewDidload
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
@situee
situee / rename_image2num.sh
Last active December 23, 2015 17:09
Batch rename image to sequence number
n=0
for file in *.jpg; do
file_name="$n.jpg"
n=$(( $n+1 ))
mv $file $file_name
done