I want to add uilabels according to mutable array at run time.
actually i have some labels texts in nsmutablearray.and i want to create label at run time and fill the text in label.plz suggest any example or provide guidance
If you have an array of NSStrings and you want to create a UILabel for each text you can do:
for(NSString *text in myArray){
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(...)];
[label setText:text];
[aView addSubview:label];
[label removeFromSuperview];
}
Related
Hi I am New in iOS Development I added UITableViewCell With Custom Cell Design in which I
added two labels in one Cell for which I added
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
label.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[self.contentView addSubView:label];
[label release];
where my First Label is masking but second Label is Sticking while repositioning table
I am Using iOS6
Please I want change text of any lbl I create it in for loop in click method
such as i want change text of the second lbl.
If any way to access uilabel by tag value.
for(int i=1,i<5,i++){
UILabel *lbl=[[UILabel alloc]initWithframe:GCrectMake(0,0,50,50)];
lbl.tag=i;
lbl.text=#"Quan:0";
[self.view addSubview:lbl];
}
-(void)click:(id)sender{
}
You just call and it will return your label.
UILabel *lbl = (UILabel *)[self.view viewWithTag:(int)];
I want to know if there is a dynamic way of populating an UIView.
For example, Consider I have a couple of UILabels aligned as rows, whose text is again dynamic.
If the text for the first UILabel has multiple lines, I know I can call
[myLabel sizeToFit];
to resize itself based on its content.
Is there an easy way of propagating this information to all the subviews (UILabels in my case) of the UIView and making them and respace them accordingly.
I hope I am clear enough.
For two labels that should be aligned as rows, you can use the CGRectGetMaxY function. For example :
UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f,20.0f, 100.0f, 200.0f)];
[first setText:#"Text to display"];
[first sizeToFit];
[self.view addSubview:first];
[first release];
UILabel *second = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(first.frame), 100.0f, 200.0f];
[second setText:#"Text to display"];
[second sizeToFit];
[self.view addSubview:second];
[second release];
UILabel *third = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, CGRectGetMaxY(second.frame), 100.0f, 200.0f];
[third setText:#"Text to display"];
etc...
You can also know the expected size of a label according to its text parameter doing this :
NSString *text = #"Text to display";
CGSize size = [text sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:10.0f] constrainedToSize:CGSizeMake(100.0f, FLT_MAX) lineBreakMode:UILineBreakModeTailTruncation];
UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, size.height)];
[first setText:text];
I hope display label as below
So I used the codes below to display label with UITableViewCellAccessoryDisclosureIndicator
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero// cellframe //SimpleTableIdentifier1
reuseIdentifier:SimpleTableIdentifier1] autorelease];
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
UILabel *labelCell =[ [[UILabel alloc]init] autorelease];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:13.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
UITableViewCellAccessoryDisclosureIndicator will display correctly
but the labelCell does not display, except I tap the row of the UITasbleViewCell
Welcome any comment
change the frame property of your UILabel,
try with changing value for x and y.
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
You could also consider to implement the accessory using accessoryView.
Implement a Custom Accessory View For UITableView in iPhone
Somehow I think doing this,
[cell.contentView bringSubviewToFront:labelCell];
will help but since there are other subviews in the table cell you will have to deal with their layout as well. Rather I suggest you create a new custom UITableViewCell subclass.
Say I have a label on the screen, and a button. When the user presses the button, it makes a new label underneath the one already there, and it can do this an unlimited number of times.
I don't need the code for making a new position. I'm just wondering how I would add a new label? Can I do the same with a bunch of variables related to it too, like say create a few NSStrings?
What you have to do is to create an UILabel view and add it to the current view as a subview programmatically.
NSMutableArray *labelArray; /* assumes it has your other labels */
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(/* your frame to locate the label */)];
newLabel.text = #"whatever";
[self.view insertSubview:newLabel below:[labelArray lastObject]];
[labelArray addObject:newLabel];
[newLabel release];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
newLabel.text = #"Some text";
[self.view insertSubview:newLabel belowSubview:originalLabel];