Xcode write NSLog result in UILabel - iphone

I use NSLog(#"%#");
And the NSLog result is '23204239423'
I don't explain you why ... it's just an example.
I want the NSLog result appear in UILabel, is it possible ?
I tried : Label.text = (#"%#"); , it doesn't work.
Do you know how i can do it ?
Thanks.

NSLog usually takes a string like:
NSLog(#"%#", string);
So instead just do this:
label.text = [NSString stringWithFormat:#"%#", string];

You have to put a variable
NSString *text = #"My Text";
label.text = text;
Or
label.text = #"Your text";

Related

How to append more text into UILabel in iOS?

I want to append more text behind UILabel in iOS.
In other languages, we can add following as:
String s += textBox.text;
We can use (+=) sign in others languages.
In Objective-C , I don't know how to add into label.
Please help me.
Try out this:
label.text = [label.text stringByAppendingString:#"your text"];
This should help you.
label.text = [NSString stringWithFormat:#"%#%#", label.text, #"your text to append"];
when you have a string which will change constantly,i advice you to use NSMutableString
NSMutableString *str=[[NSMutableString alloc]initWithString:#"aaaaaa"];
[str appendString:#"bbbbbb"];
I am used like below lines,
label.text = "My Text"
label.text = label.text! + " appended text"

Appending a string with string in ios

I am in a problem with some text sharing to evernote,sharing evernote is success,but here is my current situation with code. I have a UITableView which has some text and title for that corresponding text. When the share button is clicked it will share the text onebyone to evernote website, but the title remains static. There I get the first title name along with diffrent text. My code for this is in my tableview in rowAtIndexPath
NSMutableString *strr=[[NSMutableString alloc]initWithString:[appDelegate.indexArray objectAtIndex:indexPath.section]];
cell.textLabel.text =strr ;
cell.textLabel.text = [appDelegate.indexArray objectAtIndex:row];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:14.0];
cell.textLabel.textColor = [UIColor brownColor];
[appDelegate.notesArray objectAtIndex:row]];
//cell.detailTextLabel.text =notes;
cell.detailTextLabel.font = [UIFont fontWithName:#"Georgia" size:14.0];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = [appDelegate.notesArray objectAtIndex:row];
appDelegate.indexArray is the title content for each cell and appDelegate.notesArray has the textnote for the corresponding titles.
In shareButton click:
NSMutableString *str = [[NSMutableString alloc] initWithString:#"NOTES:"];
for (int i = 0; i<[appDelegate.notesArray count]; i++) {
NSString * aString = [[NSString alloc] initWithString:[appDelegate.notesArray objectAtIndex:i]] ;
NSString * ENML= [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%#",aString];
ENML = [NSString stringWithFormat:#"%#%#", ENML, #"</en-note>"];
NSLog(#"%#", ENML);
// Adding the content & resources to the note
[note setContent:ENML];
This will give the one by one upload of notetext.but for title I include this code
NSMutableString *strtitle = [[NSMutableString alloc] initWithString:#"myBibleApp"];
for (int i = 0; i<[appDelegate.indexArray count];i++ ) {
NSString * aStringtitle = [[NSString alloc] initWithString:[appDelegate.indexArray objectAtIndex:i]] ;
/* NSString *ENMLtitle = [NSString stringWithFormat:#"%#%#", aStringtitle];
NSLog(#"%#", ENMLtitle);*/
note.title = aStringtitle;
But here is my problem it uplode the title and text in double. That means, I have one text with title. When I click the sharebutton it uploads two times.1=2,2=4,3=6 like that.
Nut only addding the title I get this problem. If I put the title static,note.title =#"statictitle". It will not repeat the upload. How can I append the string in correct way?
Please help me.
Thanks in advance.
Two things I noted:
The use of NSMutableString is not needed. Just write for the first case
cell.textLabel.text = [appDelegate.indexArray objectAtIndex:indexPath.section];
For the 2 other cases you don't use the string at all (or it's not shown in your code).
In the for-loops, you're always overwriting aString and aStringtitle, and that even with a new alloc. Appending goes like this:
NSString *aString = #"";
for ...
aString = [aString stringByAppendingString:[appDelegate.indexArray objectAtIndex:i]];
or
aString = [aString stringByAppendingFormat:#" %#", [appDelegate.indexArray objectAtIndex:i]];
Check the NSString Class Reference for details.

Access last line while using UILineBreakModeWordWrap UILabel in iPhone

I am displaying a long string using UILabel with UILineBreakModeWordWrap. It is showing the string perfectly by wrapping text in UILabel. I want to access last line of UILabel. Does anyone know how to do this on an iPhone?
So I tried some stuff and searched a little around. What you wuld actually need is to count the word wraps and somehow detect the last string. But I didnt really figuere out how to do that.
So my sollution is something like this:
Your String //I googled some longer String
NSString *string = #"Standing across the room, I saw you smile\nSaid I want to talk to you-oo-oo for a little while\nBut before I make my move my emotions start running wild\nMy tongue gets tied and that's no lie\nLooking in your eyes\nLooking in you big brown eyes ooh yeah\nAnd I've got this to say to you\nHey!\nGirl I want to make you sweat\nSweat till you can't sweat no more\nAnd if you cry out\nI'm gonna push it some, more, more\nGirl I want to make you sweat\nSweat till you can't sweat no more\nAnd if you cry out\nI'm gonna push it\nPush it, push it some more";
Your Label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 440)];
label.font = [UIFont fontWithName:#"Helvetica" size:14];
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.text = string;
The call this Method:
NSString *result = [self getLastLineFromString:string];
NSLog(#"Result: %#", result);
getLastLineFromString: looks like this:
- (NSString *)getLastLineFromString: (NSString *)string{
NSArray *a = [string componentsSeparatedByString:#" "];
NSString *result = [a objectAtIndex:[a count]-1];
NSString *temp = #"";
int count = 1;
BOOL myBool = YES;
while (myBool) {
count++;
temp = result;
result = [a objectAtIndex:[a count] -count];
result = [NSString stringWithFormat:#"%# %#", result, temp];
NSLog(#"length: %i",[self lengthOfString:result]);
NSLog(#"result: %#",result);
//131 was a value i detected mayels, i guess u have do trick a little around to find a mathcing one for yourself
if ([self lengthOfString:result] >= 131) {
myBool = NO;
}
}
return result;
}
And The MethodlengthOfString: looks like this:
- (int)lengthOfString:(NSString *)string{
CGSize size1 = [string sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14]];
return size1.width;
}
Output:
2012-01-06 16:17:08.341 get length[5472:207] result: it
Push it, push it some more
I know this is not a perfect sollution, but it might help you.

Substring with asterisks in NSString

Hi friends i want to display credit card number in table view cell,so i want to display only last four characters like XXXXXXXXXX1111 first charecters is replaced with x or some other character how can i do this can any one help me
i have tried like bellow
NSString *CardNumber = [CardNumberValues objectAtIndex:indexPath.row];
NSRange subStringRange = NSMakeRange(0,[CardNumber length]-4);
NSMutableCharacterSet *NumSet = [NSMutableCharacterSet characterSetWithRange:subStringRange];
CardNumber = [[CardNumber componentsSeparatedByCharactersInSet:NumSet] componentsJoinedByString:#"X"];
cell.CardNumber.text = CardNumber;
but its not working can u suggest any reference or code...
Use this
NSString *CardNumber = #"12345678901234";
NSString *str_padding=#"";
NSRange subStringRange = NSMakeRange(0,[CardNumber length]-4);
str_padding =[str_padding stringByPaddingToLength:subStringRange.length withString:#"X" startingAtIndex:0];
CardNumber = [CardNumber stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%#",[CardNumber substringToIndex:subStringRange.length]] withString:str_padding];
NSLog(#"%#",CardNumber);
NSString *cardNumber = [NSString stringWithFormat: #"XXXX-XXXX-XXXX-%#", [[CardNumberValues objectAtIndex:indexPath.row] subStringWithIndex:12]];
cell.CardNumber.text = cardNumber;
I am assuming that card numbers are 16 digits.

How to get a string from a UITextField?

How would I go about getting a string from a UITextField in the iPhone SDK? I'm trying to insert it into another concatenated string.
This should do it:
NSString *myString = myTextField.text;
NSString *s = textfield.text;
yourString = [yourString appendString:s];
lblAns.text=[NSString stringWithFormat:#"%i",[[txtField1 text] intValue] + [[txtField2 text] intValue]];
you can do addition of two textboxs value like this...
for Swift 3.0
place these lines where you need
let yourString: String = ""
yourString = textField.text
Actually text attribute of textField provide the value to String.