vype
vype

Reputation: 221

How to change text in UITextView and UITextField

I need to edit my UITextView and when I make these changes I need them to reflect that in my UITextFields. Currently i can use the code below to move whatever text i write into the first UITextField is there a simple way to move each line to a separate UITextField?

Like the image, except the 1, 2 and 3 going to a different UITextField.

-(IBAction)print:(id)sender {

NSString *texto = [[NSString alloc] initWithFormat:@"%@ %@",[myTextField text],[myTextView text]];
[myTextField setText:texto];
[myTextView setText:@""];
[myTextView resignFirstResponder];

}

Like this

Really, I need to figure this out. Any help will be appreciated.

Upvotes: 1

Views: 2948

Answers (1)

Peter Heide
Peter Heide

Reputation: 519

Have your view controller conform to the UITextViewDelegate protocol.

Next, set the delegate property of your text view to your view controller.

Then, implement - (void)textViewDidChange:(UITextView *)textView, along these lines:

- (void)textViewDidChange:(UITextView *)textView
{
    // Set the text for your text fields.
    //
    // (I'm assuming your code for determining the text to put in the
    // text fields works as you intend, so I'm just copying it here.)

    NSLayoutManager *layoutManager = [textView layoutManager];
    unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
    NSRange lineRange;

    for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++)
    {
        (void) [layoutManager lineFragmentRectForGlyphAtIndex:index
                                               effectiveRange:&lineRange];

        index = NSMaxRange(lineRange);
        NSString *lineText = [textView.text substringWithRange:lineRange];
        [yourArray addObject:lineText];
    }

    textField1.text=yourArray[0];
    textField2.text=yourArray[1];
    textField3.text=yourArray[2];
    textField4.text=yourArray[3];
    textField5.text=yourArray[4];
    textField6.text=yourArray[5];
    textField7.text=yourArray[6]; 
}

Upvotes: 1

Related Questions