Problem to write more than a line in a text view - iphone

i have a UITextView in which i have to write programmatically multiple lines. When i do this :
[textView setText:#"String number 1\n"];
[textView setText:#"String number 2\n"];
i get only : String number 2 as if it's writing the line over the other. I appreciate any suggestions :)

As the name suggests, setText: is a setter: it replaces the existing text.
That's quite like if you had an int val and you were doing:
val = 5;
val = 8;
Of course at the end val will be equal to 8 and the value 5 will be lost. Same thing here.
Instead, you need to create a string that contains all the lines, and affect directly to the textView:
[textView setText:#"Line1\nLine2\nLine3\n"];
If you need to do it incrementally, say you need to add text to existing text in the textView, first retrieve the existing text, append the new line, and set the new text back:
// get the existing text in the textView, e.g. "Line1\n", that you had set before
NSString* currentText = [textView text];
// append the new text to it
NSString* newText = [currentText stringByAppendingString:#"New Line\n"];
// affect the new text (combining the existing text + the added line) back to the textView
[textView setText:newText];

setText replace whole content of textView. You better to prepare result string and than set it by single setText method call.
for example:
NSString *resultString = [NSString stringWithFormat:#"%#%#", #"String 1\n", #"String 2\n"];
[textView setText:resultString];

you can append the strings :
mTextview.text = [mTextview.text stringByAppendingString:aStr];

According to How to create a multiline UITextfield?, UITextField is single-line only. Instead, use a UITextView.
In addition, as others have pointed out, if you call setText a second time, you'll overwrite the previous value. Instead, you should do something like this:
[textView setText:#"String number 1\nString number 2"]

Related

Know where a UILabel splits a string when its multi-line property is on

Have a problem, need to assign some text to a UIButton's title. Have set the buttons line break mode to NSLineBreakByCharWrapping so that the string is separated only by characters at end of each line. But i need to insert an hyphen at the end of the line to show continuity of the word.
Heres what i tried -
// Initialize the button
titleButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
titleButton.titleLabel.backgroundColor = [UIColor yellowColor];
[titleButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15]];
// Make the string here
NSMutableString *titleString = [[NSMutableString alloc] initWithString:#"abcdefghijklmnopqrs tuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"];
// Insert hyphens
int titleLength = [titleString length];
int hyphenIndex = 19;
while (hyphenIndex<titleLength) {
UniChar charatIndex = [titleString characterAtIndex:hyphenIndex];
if ((charatIndex - ' ') != 0) { // Check if its not a break bw two words
[titleString insertString:#"-" atIndex:hyphenIndex]; // else insert an hyphen to indicate word continuity
}
hyphenIndex += 19; //Since exactly 20 char are shown in single line of the button's label.
}
//Set the hyphenated title for the button
[titleButton setTitle:titleString forState:UIControlStateNormal];
[titleString release];
This is the closest i could get.
Any help would be greatly appreciated.
Try NSAttributedString
It give a great possibilities for working with strings.
WWDC 2012-230
For example:
- (NSUInteger)lineBreakBeforeIndex:(NSUInteger)index withinRange:(NSRange)aRange
//Returns the appropriate line break when the character at the index won’t fit on the same line as the character at the beginning of the range.
your second word's length is more the length of line (width of button) thats why this happen.
pasting hyphens is not very good idea. You should split your string on parts with size that fits button's width by using than add hyphens if needed
[String sizeWithFont:font constrainedToSize:Size lineBreakMode:LineBreakMode]
The main idea is to get portion of initial string (add hyphen if word breaks), check if its width fits button's width. If width is smaller, try bigger part, else if fits -- process further part
See my answer here for how to use NSParagraphStyle and NSAttributedString to get a UILabel that breaks with hyphens: https://stackoverflow.com/a/19414663/196358

Recognizing all objects generated by a loop

This question is a bit related to one I posed previously entitled "Recognizing UIButton from a loop".
In this case, I have generated a bunch of UITextFields with a loop. When the value of one of them is changed, I'm able to recognize which textfield it was.
However, I want to then edit every textfield that was generated. Specifically, I get input from the one textfield that was edited, and I want to recalculate the other textfields based on the input and name of the recognized textfield.
How do I call for every one of the other generated, non-edited textfields to be modified?
/Vlad
Since you are already using tags, this is what viewWithTag is used for:
// Get a reference to the textfield with tag 3
UITextField *textField3 = (UITextField *)[self.view viewWithTag:3];
// Calculate your new value
float result = 4.32; // Calculate the value that you want the textfield with tag 3 to display
// Change the contents
textField3.text = [NSString stringWithFormat:#"%f", result];
store the text fields in an array, then when you want to change the value
[self.myTextFieldArray enumerateObjectsUsingBlock:^(UITextField *textField, NSUInteger idx, BOOL *stop){
if (![textField isEqual:theTextFieldThatWasEdited])
textField.text = #"whatever text you want";
}];

How to replace text in UITextView with selected range?

I want to replace the text in UITextView text in selected range. This is my question. Here i mention what i did? and what i want to do?. I have an UITextView and entered the below text in textview.
Ask question here, i have saved the range in of the text in textview. The text range is {17, 0}. I take the NSRange in -(void) textViewDidChange:(UITextView *)textView delegate.
Now i want to replace the text question with answer and i want to replace the text here with them. The UITextView.text look like this,Ask answer them` after replaced the texts.
How can i replace the texts with the selected ranges? Can you please help me? Thanks in advance.
Since iOS 7 there is the textStorage in the textView that inherits from NSMutableAttributedString so you can use those methods:
Objective-C
[self.textView.textStorage replaceCharactersInRange:withString:];
or
[self.textView.textStorage replaceCharactersInRange:withAttributedString:];
Swift
textView.textStorage.replaceCharacters(in: range, with: string)
Well... I'm not sure to understand correctly what you're trying to do, but if your goal is to change some characters in a selected range you can follow these steps:
Get your UITextView content and put it in a NSString:
NSString *textViewContent = textView.text;
Change the characters in the range you want:
NSString *newContent = [textViewContent stringByReplacingCharactersInRange:range withString:replacement];
Replace old content with new one:
textView.text = newContent;
Anyway if you just want to replace Ask question here with Ask answer them the fastest solution is just:
textView.text = #"Ask answer them";
Well solution from top of my head..
NSArray *Dividedstring = [[self.TextView.text] componentsSeparatedByString:#" question here "]; // this will divide the string into two parts ..one before question here and second after question here.
NSString * firstpart = [Dividedstring objectAtIndex:0];
NSString * secondpart = [Dividedstring objectAtIndex:0];
self.TextView.text = [NSString stringwithFormat:#"%# answer here %#",firstpart,secondpart];

How can I remove tab spacing from text for UILabel

I'm reading some text from a local xml file and displaying it in a UILabel. The text in the xml initially had tabbed spacing in it. I removed this tabbing manually in the editor but it's still showing up in the UILabel and it makes the text layout look very messy.
How can I resolve this?
Try with below
myLabel.text = [myText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet];
When you assign the text to your label you can do this:
myLabel.text = [textWithTabs stringByReplacingOccurrencesOfString:#"\t" withString:#""];
This will remove the tabs completely.
stringByTrimmingCharactersInSet:
Returns a new string made by removing
from both ends of the receiver
characters contained in a given
character set.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
NSString Class Reference

how to print a variable into a text box

hi all
I have a one variable. i want to print that varible into a text box. how to do this.
You can only display the string data in text box (UITextView),if you have other than string data it require a conversion to NSString then use the text properity of UITextView to set the display text.
The below code is for your reference.
NSString* myData = #"dISPLAY me in text box";
UITextView* myTextView = [[UITextView alloc] initWithFrame:myframe];
myTextView.text = myData;
write
NSString * str=#"Text";
myTextField.text=str;
myTextField is the name of my textField(you can write the name of your text Field).
yourTextField.text = yourVariable;
If you want to have it as a string value you can make use of stringValue
You create a UILabel object, add it as a subview of your view, and call setText: on it.
Unless you are asking how to do GUI programming in general.