iphone : Label values over write. - iphone

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]

Related

how to add UILabel within a UILabel?

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];

button action in UITableView Cell

In my app one UITableView is there. In table cell am placing two labels and one button. button height is equal to (label1+ label2) height. these are to display content. and button is for action. Here my problem is am not able to perform button action in all portion. only some part is working when clicking.
Here is the code am used to place the labels and buttons
artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];
artistLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 27.0)];
artistLabel1.textAlignment = UITextAlignmentLeft;
artistLabel1.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1];
artistLabel1.font = [UIFont boldSystemFontOfSize:15.0];
artistLabel1.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:artistLabel1];
artistLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(11.0,20.0, 170.0, 27.0)];
artistLabel2.textAlignment = UITextAlignmentLeft;
artistLabel2.textColor = [UIColor grayColor];
artistLabel2.font = [UIFont boldSystemFontOfSize:12.0];
artistLabel2.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:artistLabel2];
any one can help or suggest.
Thanks in advance.
I found answer for my question.
Because of size of the button and labels only it is not working.
Now am adjusted the width and length of button and labels its working fine.
Thank you all.
add one line before UIButton declaration..
artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
after that you can initialize your UIButton code.
Add Target action to this Button
artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[artistButton addTarget:self
action:#selector(ArtistAction)
forControlEvents:UIControlEventTouchUpInSide];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];
-(void)ArtistAction
{
NSLog("Test Artist Action");
}
I suppose you do realize that the frames you set are overlapping.
You do call [self.contentView bringSubviewToFront:artistButton]; but you add the button as a subview after so it will have no effect.
Also you add other subviews after adding the button and those will be placed above you button.
So just add the two labels first and then the button you should be able to click the button properly, though you labels will be under the button and not sure how much of them will be visible. Check the frames you are setting for the objects you are adding.
Just use for your cell tableView:didSelectRowAtIndex: method.

Adding a Background Color to a Subview with an Alpha Setting

I have a subview that shows once a button is pushed and it shows up fine. I have the subview showing a label along with it. I am just lost on how to change the background color of it and give it some transparency by adjusting it's alpha and setting the label's text color to something else. I know this is just like three to four lines of simple code but I'm at a lost now.
Here is my code:
- (IBAction)showInfo:(id)sender
{
UIView *mySubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubview:mySubview];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
label.text = #"This is a label";
[self.view addSubview:label];
}
It all runs fine, I'm just missing some pieces
Try out this code:
mySubview.backgroundColor = [UIColor colorWithRed:.5 green:.6 blue:.7 alpha.8]; // for example
label.textColor = [UIColor redColor];
Hope this help you.

How to insert more than one line of data in a single cell of UITableView?

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.

iPhone: Add "loading" subView

I am wanting to show a simple loading dialog when certain things are happening in my app. I figured I would just create a new view, add a label to that, and then set that view to a subView of my current view.
When doing this, I don't see anything!
Here is how I am writing my method:
- (void)showLoading {
UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(198, 9, 94, 27)];
txt.text = #"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[super.view addSubview:loading];
[super.view bringSubviewToFront:loading];
[loading release];
[txt release];
}
Am I doing this completely wrong?
EDIT:
I added it to the viewDidLoad method, and it works how I want:
loading = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 94, 27)];
txt.text = #"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[txt release];
[self.view addSubview:loading];
[self.view bringSubviewToFront:loading];
But when loading it from a method, it seems to lag, and not show up for a bit.
Although this doesn't directly answer your question, I'd recommend grabbing MBProgressHUD from GitHub and using that in place of a static label. Looks better, less code for you to directly maintain, etc. You can find it at http://github.com/matej/MBProgressHUD
The way I use it is by creating a subclass of UITableViewController and defining a handful of methods to show and hide the HUD view. From there, I call each relevant method when I'm loading or done loading.
Specifically, I have four methods: -hudView, -showLoadingUI, -showLoadingUIWithText:, and -hideLoadingUI.
-hudView creates a new MBProgressHUD object if one doesn't already exist, and adds it to the current view ([self.view addSubview:hudView]).
-showLoadingUI calls -showLoadingUIWithText: with a default title, -showLoadingUIWithText: just unhides the MBProgressHUD and sets a label value for it (self.hudView.labelText = #"foo";).
-hideLoadingUI hides the hudView ([self.hudView hide:YES]).
First, I don't think UIView has method called init. You may just call the super of it. The appropriate method you should call is - (id)initWithFrame:(CGRect)aRect . The frame is the position, the size of the View you want to display. More here
Another thing is why you call [super.view addSubView:], I think it should be self.view, isn't it?