Wednesday, 17 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:[UIColor redColor] range:range];
    [lblTitle setAttributedText:stringAttributed];

    Follow link for help: Apple Documentation

Thursday, 4 April 2013

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



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);

    return NO;

    return UIInterfaceOrientationMaskPortrait;

If you want to allow all orientations on a ViewController then add below code:

#pragma Orientation (override methods)
    return YES;

    return UIInterfaceOrientationMaskAll;

You can download sample code : Code Here

I followed below link for help:

Wednesday, 3 April 2013

Gradient UIButton

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,
    [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

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
                                             alpha:0.2] CGColor]];
    [cgColors addObject:(id)[[UIColor colorWithRed:1.0
                                             alpha:0.6] CGColor]];
    [cgColors addObject:(id)[[UIColor colorWithRed:1.0
                                             alpha:1.0] CGColor]];
    gradient.colors = cgColors;
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 42, 320, 2);
    layer.backgroundColor = [[UIColor colorWithRed:0.0
                                             alpha:0.7] CGColor];
    [navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    [[[navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:gradient atIndex:0];
    [[[navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:layer atIndex:2];

Final Output will look like:

I followed stackoverflow :