Mashhadi
Mashhadi

Reputation: 3024

How to create dynamic label and button on run time in iOS?

I have to create a label and button that always be centered align, user will select different item in any sequence and whenever user select an option it should be displayed in a line with a label and a cross button. when user will tap on cross button then that option will be deselected and remaining labels and buttons will set their position in center.

You may get a good idea with the following imageenter image description here

enter image description here enter image description here see "Hers" with a "X" button. here all the selected options will come

kindly guide me

Upvotes: 0

Views: 5040

Answers (3)

Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

Because I'm just that cool a person, I decided to devote some time on this issue for you :)

Download an example from my Dropbox: http://dl.dropbox.com/u/6487838/FSButtonLabelTest.zip

I'll explain the code a bit here later today.

Edit: Added some animations.

Upvotes: 2

superGokuN
superGokuN

Reputation: 1424

Adding a lable

UILabel *myLabel  =  [[UILabel alloc]init];
myLabel.frame     =  CGRectMake(50,100,100,20);
myLabel.text      =  @"Your_Text";
[self.view addSubview:myLabel];

Adding a custom button with image

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame     =  CGRectMake(210, 100, 20, 20);
[myButton setImage:[UIImage imageNamed:@"cross.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(removeFunction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];

replace the removeFunction in the button target wiht your function.. If you are not sure of the coordinates of the button or label then first go into IB and place a button where you want it to be and then in the right side check the coordinates of button in show the size inspector and use those coordinates in your code this will give you a fair idea about where your button will go after you add it programmatically... hope this will help you

Upvotes: 1

iAndroid
iAndroid

Reputation: 951

   UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(50,50,200,200)];
    [self.view addSubview:btn];
    [self.view addSubview:label];

Upvotes: 0

Related Questions