Make TextField and Label dynamically in scroll View based on arrayValue and count

Make Label :- using this we can make label dynamically based on array value 


categoryAry=[[NSArray alloc]initWithObjects:@"Object1",@"Object2",@"Object3",@"Object4",@"Object5",@"Object6",@"Object7",@"Object8",@"Object9",@"Object10", nil];




-(void)mekeAllLabelDynamicly
{
    CGFloat startX= 50.0f;
    CGFloat startY= 150.0f;
    for (int i = 0 ; i< [categoryAry count]; i++)
    {
        UILabel *label=[[UILabel alloc] init];
        label.text = [categoryAry objectAtIndex:i];
        label.tag=i;
        label.textColor = [UIColor whiteColor];
        label.backgroundColor=[UIColor clearColor];
        CGRect frame=CGRectMake(startX, startY+(31+10)*i, 100, 31);
        [label setFrame:frame];
        [self.view addSubview:label];
    }
}



Make Label+TextField and get value of textField

-(void)mekeAllTextFieldDynamicly
{
    //ScrollView Rect
    CGFloat ScrollViewX= 0.0f;
    CGFloat ScrollViewY= 100.0f;
    CGFloat ScrollViewWidth = 320.0f;
    CGFloat ScrollViewHeight = 320.0f;
    
    //Label Dimention Inside ScrollView
    CGFloat differenceBetweenTwoRecord = 10;
    
    CGFloat labelX = 10;
    CGFloat labelY = 10;
    CGFloat labelWidth = 100;
    CGFloat labelheight = 31;
    
    
    //TextField Dimention Inside ScrollView
    CGFloat textFieldX = 150;
    CGFloat textFieldY = 10;
    CGFloat textFieldWidth = 160;
    CGFloat textFieldheight = 31;
    
    
    
    UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(ScrollViewX, ScrollViewY, ScrollViewWidth, ScrollViewHeight)];
    scrollView.tag=100;
    scrollView.scrollEnabled=YES;
    scrollView.delegate=self;
    //[scrollView setContentSize:CGSizeMake(320, 6000)];

    for (int i = 0 ; i< [categoryAry count]; i++)
    {
        UILabel *label=[[UILabel alloc] init];
        label.text = [categoryAry objectAtIndex:i];
        label.textColor = [UIColor whiteColor];
        label.backgroundColor=[UIColor clearColor];
        CGRect frame=CGRectMake(labelX, labelY+(31+10)*i, labelWidth, labelheight);
        [label setFrame:frame];
        [scrollView addSubview:label];
        
        
        
        UITextField *textField=[[UITextField alloc] init];
        textField.borderStyle=UITextBorderStyleRoundedRect;
        textField.placeholder = [categoryAry objectAtIndex:i];
        textField.tag=i;
        textField.delegate=self;
        textField.returnKeyType=UIReturnKeyNext;
        CGRect TextFieldframe=CGRectMake(textFieldX, textFieldY+(31+differenceBetweenTwoRecord)*i,textFieldWidth, textFieldheight);
        [textField setFrame:TextFieldframe];
        [scrollView addSubview:textField];
        
        
        [scrollView setContentSize:CGSizeMake(320, textField.frame.origin.y+215)];
    }
    
    [self.view addSubview:scrollView];
}

//Scroll the view
- (void)scrollViewToCenterOfScreen:(UIView *)theView 
{
    UIScrollView *scr=(UIScrollView *)[self.view viewWithTag:100];

    CGFloat viewCenterY = scr.center.y;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    CGFloat availableHeight = applicationFrame.size.height -50;           // Remove area covered by keyboard
    CGFloat y = viewCenterY - availableHeight / 2.0;
    if (y < 0) {
        y = 0;
    }
    [scr setContentOffset:CGPointMake(0, y) animated:YES];
}

CGFloat animatedDistance;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 215;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:100];
    
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }    
    
    CGRect viewFrame = scrollView.frame;
    viewFrame.origin.y -= animatedDistance;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    [scrollView setContentOffset:CGPointMake(0, [scrollView contentOffset].y + .5*animatedDistance )];
    [UIView commitAnimations];
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%d",textField.tag);
    if (textField.tag == [categoryAry count]-1)
    {
        [textField resignFirstResponder];
        
        UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:100];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:1];
        [scrollView setContentOffset:CGPointMake(0,0)];
        [UIView commitAnimations];
    }
    else {
    UITextField *nexttextField=(UITextField*)[self.view viewWithTag:textField.tag+1];
    NSLog(@"%d",nexttextField.tag);
    
    [nexttextField becomeFirstResponder];
    }
        return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSMutableDictionary *textFieldValue=[[NSMutableDictionary alloc]init];

    [textFieldValue setObject:textField.text forKey:[NSString stringWithFormat:@"%d",textField.tag]];
    NSLog(@"TextField Value  >>>>  %@",[textFieldValue objectForKey:[NSString stringWithFormat:@"%d",textField.tag]]);
}




Comments

Popular posts from this blog

How to Make REST api call in Objective-C.

Launch iPhone simulator without Xcode.

Hacker Rank problem solution