Posts

Showing posts from April, 2013

Working with NSMutableAttributedString

Code snippet to demonstrate how to use NSMutableAttributedString:     UILabel *lblTitle = [[ UILabel alloc ] init ];     [lblTitle setFrame : CGRectMake ( 2 , 180 , 320 , 25 )];     [ self . view addSubview :lblTitle];          NSString *strHello = @"Hello, this is a Demo of NSMutableAttributedString" ;     NSRange range;     NSMutableAttributedString *stringAttributed = [[ NSMutableAttributedString alloc ] initWithString :strHello];     [stringAttributed addAttribute : NSFontAttributeName value :[ UIFont fontWithName : @"Arial" size : 14.0 ] range : NSMakeRange ( 0 , [strHello length ])];     range = [strHello rangeOfString : @"NSMutableAttributedString" ];     [stringAttributed addAttribute : NSFontAttributeName value :[ UIFont boldSystemFontOfSize : 13.0 ] range :range];     range = [strHello rangeOfString : @"Demo" ];     [stringAttributed addAttribute : NSForegroundColorAttributeName value :[ UICo

iOS6 Interface Orientation

In iOS6 for interface orientation  shouldAutorotateToInterfaceOrientation:  method is deprecated. If you want to make only one controller to orient in all directions but want to restrict a single controller to a specific direction like Portrait then follow: 1. Subclass a UINavigationController and inherit it 2. Override few orientation methods in ViewController in which you want or restrict the orientation support. - ( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )interfaceOrientation -( BOOL )shouldAutorotate -( NSUInteger )supportedInterfaceOrientations If you want to restrict a ViewController to rotate only in Portrait mode then add code: #pragma Orientation (override methods) - ( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )interfaceOrientation {     return (interfaceOrientation == UIInterfaceOrientationPortrait ); } -( BOOL )shouldAutorotate {     return NO ; } -( NSUInteger )supporte

Gradient UIButton

Image
To make a Gradient UIButton follow below code snippet:     UIButton *btn = [ UIButton buttonWithType : UIButtonTypeCustom ];     [btn setFrame : CGRectMake ( 90 , 120 , 140 , 32 )];     [btn setTitle : @"Gradient Button" forState : UIControlStateNormal ];     [btn setTitleColor :[ UIColor whiteColor ] forState : UIControlStateNormal ];     [btn. titleLabel setFont :[ UIFont boldSystemFontOfSize : 14 ]];     [ self . view addSubview :btn];          CAGradientLayer *layer1 = [ CAGradientLayer layer ];     NSArray *colors = [ NSArray arrayWithObjects :                        ( id )[ UIColor lightGrayColor ]. CGColor ,                        ( id )[ UIColor blackColor ]. CGColor ,                        nil ];     [layer1 setColors :colors];     [layer1 setFrame :btn. bounds ];     [btn. layer insertSublayer :layer1 atIndex : 0 ];     btn. clipsToBounds = YES ; Final output will look as follows:

NavigationBar Layer Gradient

Image
To add gradient layer to Navigationbar follow below code snippet: UINavigationBar *navigationBar = [[ UINavigationBar alloc ] initWithFrame : CGRectMake ( 0 , 0 , 320 , 44 )];     [ self . view addSubview :navigationBar];    // navigationBar.tintColor = [UIColor colorWithRed:.7 green:.5 blue:.2 alpha:1];     UINavigationItem * item = [[ UINavigationItem alloc ] initWithTitle : @"Nav Bar" ];     [navigationBar pushNavigationItem :item animated : YES ];      // Set gradient      CAGradientLayer *gradient = [ CAGradientLayer layer ];     gradient. frame = CGRectMake ( 0 , 0 , 320 , 44 );     NSMutableArray *cgColors = [[ NSMutableArray alloc ] init ];     [cgColors addObject :( id )[[ UIColor colorWithRed : 1.0                                              green : 0.0                                               blue : 0.0                                              alpha : 0.2 ] CGColor ]];     [cgColors addObject :( id )[[ UIColo