Clear a UILabel text and reset it again in xcode? - iphone

I have a UILabel set its text.But when I check for some condition and try to
change the text of UILabel if condition not met then Im getting the other other text on already present text of UILabel i.e both texts are overlapping..First I want to clear the present text and reset same UILabel to other text ..How can i do it ?
Im doing this
label.text=#"Welcome";
if(check for some condition)
{
label.text=#"Sorry";
}
But both texts are overlapping on each other...Where Im going wrong?

If you want to clear label first
label.text = #"";
and then give some value to the label
label.text = #"Sorry";
I know it's silly but sometimes it works.
Thanks.

I was having the same problem with Swift 3. It was some intermittent that at times and a text exchange look like a label. I have decided as follows:
self.label.text = "00:00:00"
self.label.setNeedsLayout()

You can try this:
label.text = nil;
And after the condition, you reassign the text.

Related

Each button click adds text to current label objective-c

I'm new to the Objective-c language. I'm trying to create an app that has a button and a label. The button should display some text which I did already. The only problem is that when I click the button, it only adds the specified text once. I want it to keep adding the same text to the label each I time I press the button.
Here is my .h file
{
IBOutlet UILabel *label;
}
-(IBAction)btnClcik:(id)sender;
Here is the .m file
-(IBAction)btnClcik:(id)sender
{
label.text=#"test";
}
To append to the existing text, use the string's concatenation method...
label.text = [label.text stringByAppendingString:#"test"];
You need to append to the string?
Then do
label.text = [label.text stringByAppendingFormat:#"%#", textToAdd];
where textToAdd is a NSString or some other valid object where %# is the correct format specifier.

how to make all fields clear when application starts iphone

I've 5 labels and 2 text fields in my iphone application when i end the application and restarts it the label and text fields will contain perviously used data. I want to make it clear all labels and text fields when application starts please any one help me Thanks in Advance.
Set the value of the fields as nil in ViewWillAppear or in ViewDidLoad methods.
Try adding this to your code:
textFieldname.stringValue = #"";
Do this in viewwillappear method:
label.text = #""; //and all other labels
txt.text = #""; //and all other textFields
Set the value of fields and labels nil i.e label.text = #""; in ViewWillAppear it'll clear fields and labels when come back to tab.

XCode - Dynamically created labels, when i change the text it changes it for the last one only

So i have a bunch of dynamically loaded labels..
Each of them has the same name because there is no telling how many there will be..
I have another method (not the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..
I need it to change the one that has a certain tag or something..
Help is much appreciated, this website is yet to let me down.
self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.
you can set tag for each label, below is sample code,
for(int i=0; i< numberOfLabels; i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
[self.view addSubview:label];
}
to access labels,
UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
Okay since you dont have any code to show i guess i have to speculate.
What i understood is that you are creating Dynamic UILabels in ur code and you want to access them. Since you have same name for all the UILabels you might me loosing the previous UILabel when every time you create a new UILabel. So in order to keep track of how many UILabel you created you must add them in an Array. Declare an NSMutableArray in your viewController.h file and make sure in the viewDidLoad u allocate it like
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray you can add object to it.
So when u create a UILabel make sure you add the same UILabel in the Array as well
for Instance
[arrForLabels addObject:yourLabel];
you can try to NSLog your Array to see its content.
Now all youu got to do is to Create a weak link like that
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel will be the UILabel to change text
tempLabel.text = #"My New Text";
It will work fine.
Feel free to ask for any issues in it.

Change Label Text

How can I change a label's text? This is what I have tried.
labelCheckBox.text = #"This is the new label";
I connected the label in Interface builder for labelCheckBox and created an outlet for it. In the Didload the label's text changed, but in another method inside the class it does not have the new value.
-(void)settext:(NSString*)newLabelText{
labelCheckBox.text = newLabelText;
// labelCheckBox = [[UILabel alloc] init];
// labelCheckBox.text = #"This is the new label";
}
I am calling the method in another class as shown here:
[New settext:#"All Transaction"];
The value of 'NewLabelText' after printing it in NSLog is: " All Transaction ". But when I assign ' LabelCheckBox.Text = NewLabelText ', I print the value of 'LabelCheckBox.Text',
and it gives empty.
First of all if you use IB you need to create outlet and connection for your UILabel. The easiest way it's dragging from your label (control + drag) to your .h file of controller class. Then synthesize it in .m file and now you can change label text as you do this in question.
the 'View' should load before setting the label text.
iOS development sometimes gives you the silence treatment like Javascript to help prevent your app from crashing. With that said, if you have created the UILabel in Interface Builder and have set an IBOutlet for it, make sure you change the text only after the UILabel is created. (i.e. after -viewDidLoad). You can set a break point to where you set the text change and a breakpoint at -viewDidLoad in the UIViewController that contains the UILabel to confirm the order of calls that's taking place.
Below code will change text in label with tag value
In scrollview use this type to change the text in label
for(UIView *views in ScrollViewMain.subviews)
{
UILabel *label = [views viewWithTag:[sender tag]+1000];
label.text=str1;
}
It will really work.
thank you

What is the best way to display long file name in UITablView

I have some source codes to display all file names in a UITablView
I noticed that if the file name too long, for example filenameabcdefghklmn.dat it will display filenameabcde...
I hope to know what is the best way to display long file name in UITablView?
Thanks
interdev
You can set the label properties of the label in which you are entering text like:
[lbl setNumberOfLines:0];
[lbl setLineBreakMode:UILineBreakModeCharacterWrap];
Now this will show the file name in the next line if the file name did not fit in one line.
You need to adjust the label height also.
Hope this helps,
Thanks,
Madhup
Customise your table cell and draw the string directly to the table cell's view.
You can use sizeWithFont:constrainedToSize: to figure out how big your string will be so you can size your cell appropriately.
This post may help you out:
http://www.ubergeek.tv/article.php?pid=143
Allow rotation to landscape mode. Or use a smaller font. Or just let the label get abbreviated with the "..." ellipsis.
There isn't a heck of a lot you can do here.
Show long name in two line in same row...
like
if([myString length] > MAX)
{
myString1 = [myString substringToIndex:MAX];
cell.textLabel.text = myString1;
mystring2 = [myString substringFromIndex:MAX];
cell.detailTextLabel.text = mystring2;
}
You can use
cell.textLabel.numberOfLines=3;
cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
Put this code in your cellForRowAtIndexPath tableview delegate methods.