This question looks very usual.. but I had a problem yesterday, I have added a label in xib and created outlet for it, I want to have multiple lines, buttons inside that label, So I have decided to add new sub labels inside it...
UILabel *label;
label = [[UILabel alloc] initWithFrame:initial_rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor whiteColor];
[xib_label addSubview:label]
When I tried like that, it was not working, then I have added the same label in self.view it works fine.. So what do I need to do when I want to add a label within a label which was added using xib. Am I missing anything here.. thanks...
I came across this situation long ago.
Just FYI :
I think the problem is the initial_rect, which is in the view's frame but out of the xib_label's frame.
The frame of label is relative to xib_label, not self.view.
You can try this :
UILabel *label;
CGRect rect = (CGRect){0, 0, 10, 10};
label = [[UILabel alloc] initWithFrame:rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor redColor];
[xib_label addSubview:label];
Change the backgroundColor to red to see the label clearly.
And if you want to show multiple lines in a label, you can :
label.numberOfLines = 0;.
you can also try after the above comments to bring subview to front [xib_lable bringSubViewToFront:label];
Related
I have a standard UITableView with three sections. At the bottom of the table I'm trying to add a tableFooterView. It seems as though there's a standard spacing between the last row in the table and the top of the tableFooterView, which is fine, but my problem is that there is a different color in this spacing that I can't get rid of. I need that spacing to match the background of my tableView.
Here's some code:
self.menuTable.backgroundColor = [UIColor redColor];
self.menuTable.backgroundView = nil;
self.menuTable.separatorColor = [UIColor clearColor];
self.menuTable.delegate = self;
self.menuTable.sectionHeaderHeight = 10.0;
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
footer.backgroundColor = self.menuTable.backgroundColor;
UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 205, 100)];
footerLabel.text = #"foo bar.";
footerLabel.textColor = [UIColor blackColor];
footerLabel.backgroundColor = self.menuTable.backgroundColor;
footerLabel.numberOfLines = 10;
footerLabel.font = [UIFont italicSystemFontOfSize:11.0];
[footerLabel sizeToFit];
[footer addSubview:footerLabel];
self.menuTable.tableFooterView = footer;
Here's a screenshot (sorry about the red, but it's the easiest way to see the differences in colors).
Whoops. Earlier I was trying to add a sectionFooterView, and had implemented tableView:heightForFooterInSection:, returning 20 for the last section. That's where that gap was coming from, and it had a default color of white. Since I'm now placing the text as the tableFooterView, I just removed that delegate callback and everything is rendering as expected.
Try this
self.menuTable.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor redColor];
I am inserting label in UIButton using addSubView
Here the label's value will be change every time.
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"]; // UserIndex will be change and new data will be load in the label
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
The Problem is : It successfully changes the label value but it is over writing it.
I am getting the new label value over the old label value.
How can I solve this ?
Every time you call [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)] you are creating new memory, not accessing the existing memory.
So, you need to have something like
if(!backTopLabel)
backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
so that you only create the memory if it doesn't exist.
However since your label isn't a property, you're essentially losing access to it anyway.
So you either need to add the label as a property of the class you're in OR tag the view so you can find it again & remove it.
Tag it & find again like this:
for(UIView* labelView in btnBackLeftCard.subviews)
{
if(labelView.tag = 100)
[labelView removeFromSuperView];
}
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
//here is where you tag the view, so you can find it again
backTopLabel.view.tag = 100;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"];
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
Assuming you have only one subview to be inserted in UIButton you can use
for(UILabel *lblViews in [btn subviews]) //Remove all subviews which are labels under button first if any
{
if(lblViews.tag == sometagno) //Compare tag number and remove only if that label found.
[lblViews removeFromSuperview];
}
backTopLabel.tag = sometagno; //Assign some tag number while adding.
[btnBackLeftCard addSubview:backTopLabel]; //Than Add your subview
This basically happens when a view gets added as subview on another view.. In your case it's been added multiple times.
Create one UILabel and then simple change it's text property.
In your case you're creating it multiple times and adding on top of the previous one.
Or if you cannot reuse previously created one, try removing it from the superview then add the latest one..
I think you are adding the subview (UILabel) again.
YOu should only change the text of UILabel
I am working in COCOS2D. If you have declared labelname globally then use
[labelName removeFromParentAndCleanup:YES]
Can I know how can we insert more than one line of data into a single cell of a UITableView,
Just Like, I want the output in a single cell to be like :
KPK
Developer
Infosystems
INDIA
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
myLabel.text = #"KPK";
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.textColor = [UIColor yellowColor];
[cell addSubview:myLabel];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
myLabel1.text = #"Developer";
myLabel1.textAlignment = UITextAlignmentCenter;
myLabel1.textColor = [UIColor yellowColor];
[cell addSubview:myLabel1];
Use \n as you are using in your string.
Set numberOfLines to 4 to allow for any number of lines.
label.numberOfLines = 4;
Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y,
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:#"KPK\nDeveloper\nInfosystems\nIndia"];
Now you just need to add the label in your cell.
[cell addSubView:label];
you can place \n in text.Not sure if that works else you can make custom cell for multiple line records.
set tablerow height and Use label in your tableview
Create a custom cell, add 4 labels in it, to display all of your required info. Checkout the following tutorial to see how you can make custom cell.
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Use a uilabel and add that one as the subview if your tableview cell.you can specify the number of lines of uilabel
label.numberOfLines = 4;
I think that the best way is creating a custom UITableViewCell implementation, or programmatically adding UILabels to the cell's contentView.
I.e.:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = #"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!
When adding the additional rows you should update the first line definition to correctly offset the new rows. Eg. the second label should have something like this:
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];
When you are done, you should implement the tableView:heightForRowAtIndexPath: method in the table's delegate, because you have to define the correct height for the new cell. In your example, for 4 labels you should implement something like this:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
if (is_one_of_my_modified_cells) {
return 35*4 + 5;
}
return 44; // default height
}
Try it and let me know.
In my app that consist of a grouped tableView,the detailTextLabel displays dates entered through a datepicker. and i use this dates to retrieve data from webservice. What i want to do is to add UILabels to detailTextLabel of the cells so that i can access the label's text property to get the dates and send request to webservice. i tried adding label to the detailTextLabel like this
cell.detailTextLabel = self.fromDate //fromDate is an instance of UILabel;
But its showing me error...
How can i fix this
You should set cell.detailTextLabel.text to modify it's contents, and possibly change the layout of cell.detailTextLabel, but not try to assign another UILabel to it.
You might want to add your own label as a subview, then do something along the lines of
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(160.0f,23.0f,150.0f,20.0f)];
lbl.textAlignment = UITextAlignmentRight;
lbl.font = [UIFont fontWithName:#"Arial" size:14.0f];
lbl.textColor = [UIColor lightGrayColor];
lbl.text = [#"Ref.No. " stringByAppendingString:refno];
[cell addSubview:lbl];
i want change title's color of navigation controller but i dont know why doesn't work !
UILabel *label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor yellowColor];
self.navigationItem.titleView = label;
i figured out my problem :
CGRect frame = CGRectMake(0, 0, 310, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:25];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor brownColor];
self.navigationItem.titleView = label;
label.text = NSLocalizedString(#"YOUR TEXT", #"");
now how can add a logo on my navigation bar ? :D
When you assign the UILabel your not giving it a size so its ending up with a frame of 0x0 - by adding [label sizeToFit]; after you've added the text you'll get what you want.
Or you can set the size of the label when you create it with initWithFrame:(CGRect)frame
Do you want to change the color of the whole navigation bar or just the UILabel in the middle of it? Assuming you want to recolor the whole bar, you can do it in Interface Builder by setting the tint color, or you can use navigationController.navigationBar.tintColor = UIColor(...)