Tap anywhere to hide Keyboard


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
    self.singleTapRecognizer.numberOfTapsRequired = 1;
    self.singleTapRecognizer.cancelsTouchesInView = NO;
}

-(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {    
    //[textField resignFirstResponder];
    [self.view endEditing:YES];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"add gesture recg");
    [self.view addGestureRecognizer:singleTapRecognizer];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSLog(@"remove gesture recg");
    [self.view removeGestureRecognizer:singleTapRecognizer];
}

Stretching an UIImage while preserving the corners




 original image


 result














myImageView.image = [myImageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(20.f, 12.f, 19.f, 19.f)];


For lower iOS version, use 

[UIImage stretchableImageWithLeftCapWidth:15 topCapHeight:13];

Blur Effect

Add QuartzCore Framework
Add this to the beginning of the AppDelegates.h


#import <QuartzCore/QuartzCore.h>

@interface CAFilter : NSObject 
@end

Add this to the didFinishLaunchingWithOptions method (before makeKeyAndVisible call)


CAFilter* filter = [CAFilter filterWithName:@"gaussianBlur"];
[filter setValue:[NSNumber numberWithFloat:5] forKey:@"inputRadius"];
window.layer.filters = [NSArray arrayWithObject:filter];

Other way you can use this in UIView:

- (IBAction)toggleBlur {
    CALayer *layer = [self.blurView layer];
    blur = !blur;
    if (blur) {
        [layer setRasterizationScale:0.3];
        [layer setShouldRasterize:YES];
    } else {
        [layer setRasterizationScale:1.0];
        [layer setShouldRasterize:NO];
    }
}


See the other static filters available at:

Resize UIImage


+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

API to get current Address

http://maps.googleapis.com/maps/api/geocode/json?latlng=1.298564,103.853973&sensor=false

UIButton Edge Inset


CGFloat spacing = 10; // the amount of spacing to appear between image and title
tabBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
tabBtn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);

Scroll Tableview to the top


NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Get Youtube Video ID


+ (NSString*) getVideoID:(NSString*)linkString
{
    NSMutableString *predicateString = [[NSMutableString alloc] initWithFormat:@"%@", linkString];
    
    NSString *regexStr = @"[a-zA-Z0-9_-]{10}";
    
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    __block NSString *retunString = @"";
    [regex enumerateMatchesInString:predicateString options:0 range:NSMakeRange(0, [predicateString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
        NSRange matchRange = [match range];
        matchRange.length = matchRange.length+1;
        //NSLog(@"matchRange.location: %d",matchRange.location); //31
        //NSLog(@"matchRange.length: %d",matchRange.length); //11
        //NSLog(@"result: %@", [predicateString substringWithRange:matchRange]); //tTfj05VNT40
        retunString = [predicateString substringWithRange:matchRange];
    }];
    
    return retunString;
}

Make a UIImage from a MKMapView or Other View


The improved function for retina display:


- (UIImage*) renderToImage
{
    if(UIGraphicsBeginImageContextWithOptions != NULL)
    {
        UIGraphicsBeginImageContextWithOptions(mapView.frame.size, NO, 0.0);
    } else {
        UIGraphicsBeginImageContext(mapView.frame.size);
    }
    [mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
    return image;
}

UITextField align left margin and clear button


you can do it by overriding two methods:

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
the first one to align the placeholder and the second one for the editable text, so here is the code following your first answer


@implementation MYTextField


- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
    return inset;
}


- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
    return inset;
}



- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect inset = CGRectMake(bounds.origin.x + 113, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
    return inset;
}