Define Maximum Characters in Textfield


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 80) ? NO : YES;
}

Change UITextField’s Placeholder Color

[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];

Set Rounded Corner to Image


UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];


// You can even add a border
[l setBorderWidth:4.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];

How to post Tweets with iOS5


  1. Add Twitter framework to your project
  2. #import "Twitter/TWTweetComposeViewController.h"
  3. Add this code:
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    
    [twitter setInitialText:@"It's really that simple!"];
    [twitter addImage:[UIImage imageNamed:@"twitter.png"]];

    [self presentViewController:twitter animated:YES completion:nil];
       
    twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
        
        if(res == TWTweetComposeViewControllerResultDone)
        {
            
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Succes!" message:@"Your Tweet was posted succesfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
            
            
        }else if(res == TWTweetComposeViewControllerResultCancelled)
        {
            
            UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
            
        }
        
        [self dismissModalViewControllerAnimated:YES];
        
    };

Disable or Hide Button in UIActionSheet


UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share"
delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
otherButtonTitles:@"Send Message (SMS)", @"Send E-Mail", @"Print", @"Twitter", nil];
    [actionSheet showInView:self.view];
    for(UIView *v in [actionSheet subviews])
    {
        if([[v description] hasPrefix: @"<UIThreePartButton"] )
        {
            if ([v respondsToSelector:@selector(title)])
            {
                NSString* title = [v performSelector:@selector(title)];
                if ([title isEqualToString:@"Twitter"])
                {
                    //v.hidden = YES;
                    ((UIButton*)v).enabled = NO;
                }
            }
        }
    }
    [actionSheet release];

Check Current Orientation


if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft){
        //do something or rather
        NSLog(@"landscape left");
    }
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight){
        //do something or rather
        NSLog(@"landscape right");
    }
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait){
        //do something or rather
        NSLog(@"potrait");
    }
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown){
        //do something or rather
        NSLog(@"potrait upside down");
    }

Or you can also use this:

[[UIDevice currentDevice] orientation]

Swipe Gesture Recognizer


UISwipeGestureRecognizer * swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self;
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];
swipeRight = nil;

Make Animation Slow at the Beginning

[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
mainImage.alpha = 0.0f;
[UIView commitAnimations];

Details:
UIViewAnimationCurveEaseInOut: The default Value, slow at beginning and end of animation
UIViewAnimationCurveEaseIn: Slow at beginning, the speeds up
UIViewAnimationCurveEaseOut: Animation slows at end of animation
UIViewAnimationCurveLinear: Uniform speed throughout duration

Sort Array with comparator

For a mutable array of dictionaries:

NSMutableArray *aMutableArray = [NSMutableArray arrayWithObjects:
         [NSDictionary dictionaryWithObjectsAndKeys:
          @"A", @"name",
          [NSNumber numberWithInt:6], @"value", nil],
         [NSDictionary dictionaryWithObjectsAndKeys:
          @"C", @"name",
          [NSNumber numberWithInt:2], @"value", nil],
         [NSDictionary dictionaryWithObjectsAndKeys:
          @"B", @"name",
          [NSNumber numberWithInt:4], @"value", nil], nil];
In this case case here's how you sort it by value:


NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];



Otherwise:


NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES comparator:^(id left, id right) {
float v1 = [left floatValue];
float v2 = [right floatValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}];

[data sortUsingDescriptors:[NSArray arrayWithObject:lastNameDescriptor]];

sort an array of strings alphabetically when the strings contains åäö

NSArray *strings=[NSArray arrayWithObjects:@"as", @"ol", @"st", @"br", @"ög", @"år", @"ös", nil];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"];

NSArray *sorted=[strings sortedArrayUsingComparator:^(NSString *first, NSString *second) {
return [first compare:second
options:0
range:NSMakeRange(0, [first length])
locale:locale];
}];

for (NSString *s in sorted) { NSLog(@"%@", s); }

output:
as br ol st år ög ös