【问题标题】:How can I add auto indentation to UITextView when user type new line?用户键入新行时,如何向 UITextView 添加自动缩进?
【发布时间】:2015-02-04 17:37:24
【问题描述】:

当用户键入新行时,如何为UITextView 添加自动缩进?示例:

line1
  line2 <user has typed "Enter">
  <cursor position>
    line3 <user has typed "Enter">
    <cursor position>

【问题讨论】:

  • 那些对简单、好的和真实的问题设置负面影响的人是谁?讨厌他们。
  • 是的,我同意。越来越荒谬了。
  • 我很快就会给你答复。
  • 好吧,我不确定我是否明白你到底想要做什么......你能用文字解释一下吗?所以用户在第一行输入,然后绕到第二行,然后按回车键,光标转到下一行……但是第 2 行是否自动缩进?在第 2 行之后的行中是否写有 的内容?以及如何获得第 3 行的缩进?等等……
  • 那么这个问题其实很复杂。首先要识别前行开头的文本并非易事...您可以尝试使用stackoverflow.com/a/14413484/2274694 之类的方法来识别上面行中的文本,然后计算空格,并将这些空格替换为“\ t" 在我下面写的方法中。

标签: ios cocoa-touch uitextview indentation auto-indent


【解决方案1】:

虽然在这种情况下,看起来 OP 实际上并不是在寻找标准缩进,但我将把它留给未来的答案寻求者。

以下是在每个换行符后自动添加缩进的方法。我从我最近的类似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;
}

【讨论】:

  • 非常感谢您的回答!但是我怎么能把这么多的空格放在上面呢?
  • 它有效。但是如何防止光标位置?在[textView setText: mutableText] 调用后,它始终显示在文本的末尾。
  • @Altaveron 哦,当然...您可以使用textView.selectedRange 设置光标位置。我会尽快更新更具体的...
  • @Altaveron 好的,已添加代码! (并且代码已修复......我意识到之前有点草率,但你自己完成它做得很好!;))
  • 由于滚动问题而无法正常工作。
【解决方案2】:

对于第一行,您必须编写以下代码:

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

【讨论】:

    猜你喜欢
    • 2020-07-13
    • 2010-12-06
    • 1970-01-01
    • 2019-05-10
    • 2018-06-18
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多