2

I am creating two label programmatically using this code..

-(void)addLabel:(id)sender
{
    ExampleAppDataObject* theDataObject = [self theAppDataObject]; 
    theDataObject.count = theDataObject.count+1;
    NSLog(@"count is :%i",theDataObject.count);

    if (theDataObject.count == 2) {
        addLabel.enabled = NO;
    }
    if (theDataObject.count == 1) {
        CGRect imageFrame = CGRectMake(10, 10, 150, 80);
        labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
        blabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 100, 100)];

        blabel.text = @"Write here";
        //alabel.text = self.newsAsset.title;
        blabel.adjustsFontSizeToFitWidth = NO;
        blabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        blabel.font = [UIFont boldSystemFontOfSize:18.0];
        blabel.textColor = [UIColor blueColor];    
        // alabel.shadowColor = [UIColor whiteColor];
        // alabel.shadowOffset = CGSizeMake(0, 1);
        blabel.backgroundColor = [UIColor clearColor];
        blabel.lineBreakMode = UILineBreakModeWordWrap;
        blabel.numberOfLines = 10;
        blabel.minimumFontSize = 8.;
        blabel.adjustsFontSizeToFitWidth = YES;
        [blabel sizeToFit];
        labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;



        // enable touch delivery
        blabel.userInteractionEnabled = YES;

        //tao gasture recognizer for label
        UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blabelTap:)]; 
        doubleTap.numberOfTapsRequired = 2; 
        [blabel addGestureRecognizer:doubleTap];

        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
                                                          initWithTarget:self
                                                          action:@selector(longPress:)];
        [longPressGesture setMinimumPressDuration:1];
        [blabel addGestureRecognizer:longPressGesture];

        //Calculate the expected size based on the font and linebreak mode of your label
        CGSize maximumLabelSize = CGSizeMake(296,9999);

        CGSize expectedLabelSize = [greetString sizeWithFont:blabel.font 
                                           constrainedToSize:maximumLabelSize 
                                               lineBreakMode:blabel.lineBreakMode]; 


        //adjust the label the the new height.
        CGRect newFrame = blabel.frame;
        newFrame.size.height = expectedLabelSize.height+40;
        newFrame.size.width = expectedLabelSize.width+40;

        blabel.frame = newFrame;
        labelResizableView.frame = newFrame;

        labelResizableView.contentView = blabel;
        labelResizableView.delegate = self;
        labelResizableView.tag =2;
        [self.view addSubview:labelResizableView];
    }else if (theDataObject.count == 2) {
        CGRect imageFrame = CGRectMake(10, 10, 150, 80);
        labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
        clabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 100, 100)];

        clabel.text = @"Write here";
        //alabel.text = self.newsAsset.title;
        clabel.adjustsFontSizeToFitWidth = NO;
        clabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        clabel.font = [UIFont boldSystemFontOfSize:18.0];
        clabel.textColor = [UIColor blueColor];    
        // alabel.shadowColor = [UIColor whiteColor];
        // alabel.shadowOffset = CGSizeMake(0, 1);
        clabel.backgroundColor = [UIColor clearColor];
        clabel.lineBreakMode = UILineBreakModeWordWrap;
        clabel.numberOfLines = 10;
        clabel.minimumFontSize = 8.;
        clabel.adjustsFontSizeToFitWidth = YES;
        [clabel sizeToFit];
        labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;



        // enable touch delivery
        clabel.userInteractionEnabled = YES;

        //tao gasture recognizer for label
        UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clabelTap:)]; 
        doubleTap.numberOfTapsRequired = 2; 
        [clabel addGestureRecognizer:doubleTap];

        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
                                                          initWithTarget:self
                                                          action:@selector(longPress:)];
        [longPressGesture setMinimumPressDuration:1];
        [clabel addGestureRecognizer:longPressGesture];

        //Calculate the expected size based on the font and linebreak mode of your label
        CGSize maximumLabelSize = CGSizeMake(296,9999);

        CGSize expectedLabelSize = [greetString sizeWithFont:clabel.font 
                                           constrainedToSize:maximumLabelSize 
                                               lineBreakMode:clabel.lineBreakMode]; 


        //adjust the label the the new height.
        CGRect newFrame = blabel.frame;
        newFrame.size.height = expectedLabelSize.height+40;
        newFrame.size.width = expectedLabelSize.width+40;

        clabel.frame = newFrame;
        labelResizableView.frame = newFrame;

        labelResizableView.contentView = clabel;
        labelResizableView.delegate = self;

        labelResizableView.tag=3;
        [self.view addSubview:labelResizableView];
    }


}

And when user long press button than it will be deleted...

- (void)longPress:(UILongPressGestureRecognizer *)longPressGesture {

    if (longPressGesture.state == UIGestureRecognizerStateEnded) {
        //NSLog(@"Long press Ended");

       // NSLog(@"blabel long pressed");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete Label" message:@"Want delete label" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
        [alert show];

    }
    else {
        //NSLog(@"Long press detected.");
    }


}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        ExampleAppDataObject* theDataObject = [self theAppDataObject];
        if (theDataObject.count!=0) {
            theDataObject.count = theDataObject.count-1;
        }
        addLabel.enabled = YES;
        [labelResizableView removeFromSuperview];
       // NSLog(@"yes btn tapped");
    }
}

but now when i longpree blabel than still clabel is deleted and it will never delete blabel.thanx in advance.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264

5 Answers5

5

Use the Tag property to remove the labelResizableView.

-(void)addLabel:(id)sender
{
  labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame];
  labelResizableView.tag = 100;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        ExampleAppDataObject* theDataObject = [self theAppDataObject];
        if (theDataObject.count!=0) {
            theDataObject.count = theDataObject.count-1;
        }
        addLabel.enabled = YES;
        UILabel *tempLabel = (UILabel *)[self.view viewWithTag:100];
        if(tempLabel)
            [tempLabel removeFromSuperview];

    }
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • This code is working, but its only work for one label.i have given tag 100 to blabel and tag 101 to clabel, if i give another tag in templabel than it will delete both label.any tip. –  Nov 28 '12 at 06:15
  • i have done it bit differently but still for delete i have used your logic,thanx. –  Nov 28 '12 at 06:54
  • 1
    Thanks. This is still working on Xcode 11, iOS 13. Very short, effective fix. – user3741598 Apr 14 '20 at 22:44
1

hope this code help you :)

NSArray *subArray =  [self.view subviews];
    if([subArray count] != 0) {
        for(int i = 0 ; i < [subArray count] ; i++) {           
    [[subArray objectAtIndex:i] removeFromSuperview];        
 }
    }
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
Rushabh
  • 3,208
  • 5
  • 28
  • 51
1

To add control in your view:

[self.view addsubview:yourcontrolid];

ex:

[self.view addsubview:labelid];

To add control from your view:

[controlid removefromsuperview];

ex

[labelid removefromsuperview];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
mano
  • 184
  • 1
  • 8
0

you are adding with :

    [self.view addSubview:labelResizableView];

than remove it the labelResizableView, and release the clabel or blabel, whatever is in your case.

Maybe this gives an example

Community
  • 1
  • 1
0

It is because your code

else if (theDataObject.count == 2) {

is calling and in this code you are adding

 labelResizableView.contentView = clabel;  

and then you are adding this to you view

[self.view addSubview:labelResizableView];  

So when you are deleting labelResizableView

[labelResizableView removeFromSuperview];

So the result is you are adding labelResizableView 2 times and remove the labelResizableView which have clabel.

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74