4

How can I add auto indentation to UITextView when user type new line? Example:

line1
  line2 <user has typed "Enter">
  <cursor position>
    line3 <user has typed "Enter">
    <cursor position>
Daniyar
  • 2,975
  • 2
  • 26
  • 39
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • 1
    Who are those people who set minuses to the simple, good and real questions? Hate them. – Dmitry Dec 06 '14 at 18:06
  • 2
    Yes, I agree. It's getting ridiculous. – Lyndsey Scott Dec 06 '14 at 18:18
  • 1
    And I'll have an answer for you shortly. – Lyndsey Scott Dec 06 '14 at 18:19
  • 1
    OK, I'm not sure I understand what you're trying to do after all... Could you explain it in words? So the user types on line one and it wraps around to line two then the press enter and the cursor goes to the next line... But is line 2 automatically indented? And is anything written on the line after line 2 where it says ? and how to you get the indent for line 3? etc... – Lyndsey Scott Dec 06 '14 at 18:37
  • New line just starts after the same count of spaces as on the upper line. Nothing more. But cursor must be after all inserted spaces. – Dmitry Dec 06 '14 at 19:04
  • By "upper line" you mean the line before? So basically if the user doesn't "indent" a line, the next line wouldn't be indented? And if a line is indented, but the user adds additional spaces after that indent, the next line will have that cumulative amount of indented spaces? – Lyndsey Scott Dec 06 '14 at 19:11
  • 1
    This question is actually really complicated then. Firstly to identify the text at the start of the line before is no easy feat... You could try something like http://stackoverflow.com/a/14413484/2274694 to identify the text that's in the line above, then count up the spaces, and replace those spaces with "\t" in the method I wrote out below. – Lyndsey Scott Dec 06 '14 at 19:27

2 Answers2

3

Although it seems as if the OP isn't in fact looking for standard indentation in this case, I'm leaving this up for future answer seekers.

Here's how you can automatically add an indent after every newline entry. I've adapted this answer from my similar recent answer about automatically adding bullet points at every newline.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    // If the replacement text is "\n" thus indicating a newline...
    if ([text isEqualToString:@"\n"]) {

        // If the replacement text is being added to the end of the
        // text view's text, i.e. the new index is the length of the
        // old text view's text...
        if (range.location == textView.text.length) {
            // Simply add the newline and tab to the end
            NSString *updatedText = [textView.text stringByAppendingString:@"\n\t"];
            [textView setText:updatedText];
        }

        // Else if the replacement text is being added in the middle of
        // the text view's text...
        else {

            // Get the replacement range of the UITextView
            UITextPosition *beginning = textView.beginningOfDocument;
            UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
            UITextPosition *end = [textView positionFromPosition:start offset:range.length];
            UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];

            // Insert that newline character *and* a tab
            // at the point at which the user inputted just the
            // newline character
            [textView replaceRange:textRange withText:@"\n\t"];

            // Update the cursor position accordingly
            NSRange cursor = NSMakeRange(range.location + @"\n\t".length, 0);
            textView.selectedRange = cursor;

        }

        // Then return "NO, don't change the characters in range" since
        // you've just done the work already
        return NO;
    }

    // Else return yes
    return YES;
}
Community
  • 1
  • 1
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
2

For the first line, you would have to write this code:

- (void)textViewDidBeginEditing(UITextView *)textView
{
    if ([textView.text isEqualToString:@""])
    {
        [textView setText:@"\t"];
    }
}