Skip to content

Instantly share code, notes, and snippets.

View psobko's full-sized avatar

Piotrek Sobkowski psobko

View GitHub Profile
@psobko
psobko / ScreenCapture.m
Last active December 10, 2015 07:38
Take a screenshot of a given view and optionally crop it.
+(UIImage*)TakeScreenshotOfView:(UIView*)view Cropping:(CGRect)dimensions
{
CGFloat scale = 1.0f;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
@psobko
psobko / resizeFBLogin
Created December 29, 2012 05:16
Resize FBLoginView on iOS SDK
- (void)resizeLoginView:(FBLoginView*)loginview
{
for(UIView * view in loginview.subviews)
{
if([view isKindOfClass:[UILabel class]])
{
view.frame = CGRectMake( view.frame.origin.x-23, view.frame.origin.y, view.frame.size.width/2, view.frame.size.height /2);
}
else
{
@psobko
psobko / UIViewPositioning.m
Last active December 10, 2015 08:38
UIView Positioning
//center horizontally or vertically
subView.center = CGPointMake(parentView.frame.size.width / 2, parentView.frame.size.height / 2);
//center horizontally + vertically
subView.center = [parentView convertPoint:parentView.center fromView:parentView.superview];
@psobko
psobko / AddErrorBox
Created June 16, 2013 03:58
Draws a box around a view and then animates it to grow vertically. Originally I was going to use this to display errors underneath UITexFields.
-(void)addBox
{
self.layer.masksToBounds = NO;
CALayer *errorLayer;
errorLayer = [CALayer layer];
errorLayer.backgroundColor = [UIColor blueColor].CGColor;
errorLayer.frame = CGRectMake(0,0, self.frame.size.width, self.frame.size.height);
errorLayer.cornerRadius = 0.0f;
CALayer *bgLayer = [CALayer layer];
bgLayer.frame = errorLayer.frame;
@psobko
psobko / UIScrollView Max Bounce
Created August 28, 2013 16:15
Control how far a UIScrollView can bounce
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y < -50)
{
scrollView.contentOffset = CGPointMake(0, -50);
}
}
@psobko
psobko / UIScrollView Detect Bounce Direction
Created August 28, 2013 16:17
Detect which direction a UIScrollView is bouncing
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float scrollOffsetY = scrollView.contentOffset.y;
if(scrollOffsetY < 0)
{
//Pulling down
}
else if(scrollOffsetY > scrollView.contentSize.height - scrollView.frame.size.height)
{
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
@psobko
psobko / gist:6705823
Last active December 23, 2015 22:49
Color a word in NSAttriburedString
//Modified from http://stackoverflow.com/questions/8533691/how-to-get-all-nsrange-of-a-particular-character-in-a-nsstring
- (NSMutableAttributedString*) setColor:(UIColor*)color word:(NSString*)word inText:(NSMutableAttributedString*)mutableAttributedString {
NSUInteger count = 0, length = [mutableAttributedString length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [[mutableAttributedString string] rangeOfString:word options:0 range:range];
@psobko
psobko / gist:6720079
Created September 26, 2013 20:30
Detect when back is tapped on navigation controller without subclassing
//http://stackoverflow.com/questions/12748911/do-things-when-back-button-is-pressed
-(void)viewWillDisappear:(BOOL)animated {
NSUInteger ind = [[self.navigationController viewControllers] indexOfObject:self];
if (ind == NSNotFound) {
// do something, we're coming off the stack.
}
}
@psobko
psobko / 0_reuse_code.js
Created September 27, 2013 21:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console