How to append more text into UILabel in iOS? - iphone

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"

Related

Updated substring in NSAttributedString

How I can replace substring in NSMutableAttributedString without adding static range number?
I have a label with this text: #"12 friends", I want to replace 12 (number of friends) with another number (and use the same attributes for this substring) once that it will come from the server, and I can not use the below approach since the number of digits is unknown:
/*wrong approach*/
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
[mutableAttributedString replaceCharactersInRange:NSMakeRange(0, 2) withString:counter];
[label setAttributedText:mutableAttributedString];
If the label will always read "x friends" then why not just use a formatted string and pass in the number of friends as a parameter. Of course you could make this whole thing variable for localization, etc. but the basic idea is this:
NSInteger numberFromServer = ...
NSString *string = [NSString stringWithFormat:#"%d friend%#",numberFromServer,((numberFromServer != 1) ? #"s" : #"")];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[label setAttributedText:attributedString];

Xcode write NSLog result in UILabel

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

Get first sentence of textview

I am trying to get the first sentence of a text view. I have the following code but am getting an out of bounds error. Thank You. Or are there any ways that aren't really complex.
-(IBAction)next:(id)sender
{
NSRange ran = [[tv.text substringFromIndex:lastLocation] rangeOfString:#". "];
if(ran.location != NSNotFound)
{
NSString * getRidOfFirstHalfString = [[tv.text substringFromIndex:lastLocation] substringToIndex:ran.location];
NSLog(#"%#",getRidOfFirstHalfString);
lastLocation+=getRidOfFirstHalfString.length;
}
How about:
NSString *finalString = [[tv.text componentsSeparatedByString:#"."] objectAtIndex:0] // Get the 1st part (left part) of the separated string
Go through the textview's text and divide the text into separate components where you find a period by calling componentsSeperatedByString on tv.text. You want the first sentence, which would be the 0th object in the array.
I know you've already accepted an answer to this question, but you might want to consider using the text view's tokenizer instead of just searching for the string ". " The tokenizer will automatically handle punctuation like !, ?, and closing quotes. You can use it like this:
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextRange *range = [tokenizer rangeEnclosingPosition:textView.beginningOfDocument
withGranularity:UITextGranularitySentence
inDirection:UITextStorageDirectionForward];
NSString *firstSentence = [textView textInRange:range];
If you want to enumerate all of the sentences, you can do it like this:
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextPosition *start = textView.beginningOfDocument;
while (![start isEqual:textView.endOfDocument]) {
UITextPosition *end = [tokenizer positionFromPosition:start toBoundary:UITextGranularitySentence inDirection:UITextStorageDirectionForward];
NSString *sentence = [textView textInRange:[textView textRangeFromPosition:start toPosition:end]];
NSLog(#"sentence=%#", sentence);
start = end;
}
Try checking that the substring was actually found.
NSRange ran = [tv.text rangeOfString:#". "];
if(ran.location != NSNotFound)
{
NSString * selectedString = [tv.text substringToIndex:ran.location];
NSLog(#"%#",selectedString);
}
You could alternatively try using NSScanner like this:
NSString *firstSentence = [[NSString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:tv.text];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"."];
[scanner scanUpToCharactersFromSet:set intoString:&firstSentence];
I'm not sure if you want this, but since you want the first sentence, you could append a period (you probably know how to do this, but it doesn't hurt to show it anyway):
firstSentence = [firstSentence stringByAppendingFormat:#"."];
Hope this helps!
PS: If it didn't work for you, maybe the text view doesn't actually contain any text.

problems with NSString

I am sorry of the question, but i have searched but i have not found the solution.
I have a textField, and i would like to add a caractère to the text of my textField. how to do this, please ?
if ( myConddition) {
otherTextField.text = myTextField.text ( a would like to add a caractér("1") to the first position ).
thanks for your answers
otherTextField.text = [NSString stringWithFormat:#"1%#", myTextField.text];
Use below
NSMutableString* myString = [NSMutableString alloc] appendFormat:#"%#%#",#"1", myTextField.text]];
myTextField.text = myString;
[myString release];
otherTextField.text = [#"1" stringByAppendingString:myTextField.text];
Assuming you've created #property declarations...
[[self otherTextField] setText:[NSString stringWithFormat:#"1%#", [[self myTextField] text]]];
P.S. Eww at the use of dot notation by you boys and girls.

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.