Sunday, August 25, 2013

Using Colourful Texts in Default Navigation Bar in iOS

It is a very common problem for iOS Developers that we want to use Colourful texts in the Default Navigation Bar but do not know how to do it. Today I am showing you how to use colourful text in the default Navigation Bar. We have to do it programmatically.



- (void)viewDidLoad
{
    [super viewDidLoad];
        
    self.title=@"You Tube Vedio";
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        
        titleView.textColor = [UIColor redColor]; // Change to desired color
        
        self.navigationItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = self.title;
    [titleView sizeToFit];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
}





I hope it will be helpful to you . 

Happy Coding 
Mahboob

Wednesday, August 21, 2013

Displaying Youtube Videos in iOS App

Now a day youtube is a very famous website for videos not only for entertainment but also for tutorials. If you want to play youtube video using iOS app you have to use Web view  and write the simple code in viewDidLoad method given below:


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *html = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {    background-color: transparent;\
    color: white; \
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <iframe class=\"youtube-player\" width=\"300\" height=\"300\" src=\"http://www.youtube.com/embed/5hyy2JfuXko\" frameborder=\"0\" allowfullscreen=\"true\"></iframe>\
    </body></html>";
    
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:CGRectMake(10.0, 80.0, 300.0, 300.0)];
    videoView.backgroundColor=[UIColor blackColor];
    videoView.scrollView.bounces=NO;
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
}

Then you can run the app and play youtube videos :

Create Grid Like xl sheet using iOS App


In this section I would like to discuss how to create gird like xl sheet using iOS Application. Well we have create it in a custom way .

  • At first we will take an image view for the header of the grid. We will set the header image in the imageView. 
  • Then we will use textBox and set the textBox programatically in effective way . 
  • We have to keep in mind that in which position we are setting the header image. According to the header image we will take the dimension and set the textBox programatically. 
  • Another thing is the grid size is dynamic according to the user choice. 
The source code is given below:

headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(055076881)];
    headerImageView.image=[UIImage imageNamed:@"R_11.png"];
    [scrollView addSubview:headerImageView];
    int x=0;
    int y=633;
    int count2=[self getCFLNo];
    NSLog(@"cflNo : %d",count2);
    
    for(int p=1 ;p<=count2;)
    {
        x=0;
        blankCellImageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 24050)];
        blankCellImageView.image=[UIImage imageNamed:@"blankCell.png"];
        blankCellImageView.alpha=0.8;
        [scrollView addSubview:blankCellImageView];
        x=x+110;
        // y=y+52;
        large15WTextBField=[[UITextField alloc]initWithFrame:CGRectMake(x, y, 240,50)];
        large15WTextBField.font=font;
        large15WTextBField.backgroundColor=[UIColor whiteColor];
        large15WTextBField.layer.borderColor=[[UIColor blackColor]CGColor];
        large15WTextBField.layer.borderWidth2.0f;
        [large15WTextBField resignFirstResponder];
        large15WTextBField.delegate=self;
        // serialNumberTextField.textAlignment=UITextAlignmentCenter;
        [scrollView addSubview:large15WTextBField];
        x=x+110;
        //y=y+52;
        small15WBTextField=[[UITextField alloc]initWithFrame:CGRectMake(x, y, 240,50)];
        small15WBTextField.font=font;
        small15WBTextField.backgroundColor=[UIColor whiteColor];
        small15WBTextField.layer.borderColor=[[UIColor blackColor]CGColor];
        small15WBTextField.layer.borderWidth2.0f;
        [small15WBTextField resignFirstResponder];
        // roomNameTextField.textAlignment=UITextAlignmentCenter;
        small15WBTextField.delegate=self;
        [scrollView addSubview:small15WBTextField];
        x=x+110;
        // y=y+52;
        large20WBTextField=[[UITextField alloc]initWithFrame:CGRectMake(x, y, 240,50)];
        large20WBTextField.font=font;
        large20WBTextField.backgroundColor=[UIColor whiteColor];
        large20WBTextField.layer.borderColor=[[UIColor blackColor]CGColor];
        large20WBTextField.layer.borderWidth2.0f;
        [large20WBTextField resignFirstResponder];
        large20WBTextField.delegate=self;
        //  masterDeviceTextField.textAlignment=UITextAlignmentCenter;
        [scrollView addSubview:large20WBTextField];
        x=x+110;
        //y=y+52;
        large15WTextEField=[[UITextField alloc]initWithFrame:CGRectMake(x, y, 240,50)];
        large15WTextEField.font=font;
        large15WTextEField.backgroundColor=[UIColor whiteColor];
        large15WTextEField.layer.borderColor=[[UIColor blackColor]CGColor];
        large15WTextEField.layer.borderWidth2.0f;
        [large15WTextEField resignFirstResponder];
        large15WTextEField.delegate=self;
        //   slaveDeviceTextField1.textAlignment=UITextAlignmentCenter;
        [scrollView addSubview:large15WTextEField];
        x=x+110;
        // y=y+52;
        small15WETextField=[[UITextField alloc]initWithFrame:CGRectMake(x, y, 240,50)];
        small15WETextField.font=font;
        small15WETextField.backgroundColor=[UIColor whiteColor];
        small15WETextField.layer.borderColor=[[UIColor blackColor]CGColor];
        small15WETextField.layer.borderWidth2.0f;
        [small15WETextField resignFirstResponder];
        small15WETextField.delegate=self;
        //  slaveDeviceTextField2.textAlignment=UITextAlignmentCenter;
        [scrollView addSubview:small15WETextField];
        x=x+110;
        NSLog(@"x: %d",x);
        //  y=y+52;
        large20WETextField=[[UITextField alloc]initWithFrame:CGRectMake(x, y, 240,50)];
        large20WETextField.font=font;
        large20WETextField.backgroundColor=[UIColor whiteColor];
        large20WETextField.layer.borderColor=[[UIColor blackColor]CGColor];
        large20WETextField.layer.borderWidth2.0f;
        [large20WETextField resignFirstResponder];
        large20WETextField.delegate=self;
        //    slaveDeviceTextField3.textAlignment=UITextAlignmentCenter;
        [scrollView addSubview:large20WETextField];
        if(x==660)
        {
            y=y+52;
        }
        
        p++;
        
    }

For example the user wants to use 6 columns in the grid , then the grid will be this.....

Wednesday, June 19, 2013

Detect and show alert if user skip any text field to give entry

I think it is an interesting tricks to detect if used skip any text field to give entry. We can do this using a TextField delegate method

- (void)textFieldDidEndEditing:(UITextField *)textField;

Suppose your app has so many text fields  and it is very common that user may skip any text field to give entry. You can change the background colour of the text field immediately the user skips. Then the user will be aware of it .

The code is given below.



- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if([textField.text length]==0)
    {
        textField.backgroundColor=[UIColor yellowColor];
    }
    else
    {
        textField.backgroundColor=[UIColor whiteColor];
    }
    
}

I hope this post will be helpful to you . 

Happy Coding 

Mahboob 

Monday, May 27, 2013

Make your app colourful by using colour code

We all iOS Developers know that we can use some distinct colours in component's background or tint colour.  If we use custom colour code the apps will be more lucrative. I am giving a simple example,

I create a segmented control programatically and set the background colour using the colour code.
In viewDidLoad method I set the  navigations bar's tint colour and background the view using colorWithHexString method which returns colour according to the hexadecimal form of the colour code. I hope it will be helpful to you.


- (void)viewDidLoad
{
    [super viewDidLoad];
     self.navigationController.navigationBar.hidden=NO;
  [self.navigationController.navigationBar setTintColor:[self colorWithHexString:@"843C44"]];
    [self.view setBackgroundColor:[self colorWithHexString:@"845C44"]];
    

}



-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    
    if ([cString length] < 6) return [UIColor grayColor];
    
    
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    
    if ([cString length] != 6) return  [UIColor grayColor];
    
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}


Now you can see how colourful your apps will be.

Happy Coding

Mahboob