如何在UITextView中的每个新行中添加无序列表或项目符号点 [英] How do I add an unordered list or a bullet point every new line in a UITextView

查看:79
本文介绍了如何在UITextView中的每个新行中添加无序列表或项目符号点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上我想将一个无序列表添加到 UITextView .并向另一个 UITextView 添加一个有序列表.

So basically I want to add an unordered list to a UITextView. And to another UITextView I want to add an ordered list.

我尝试使用此代码,但它仅在用户第一次按Enter键后才给我一个要点(仅此而已),我什至不能退格.

I tried using this code, but it only gave me a bullet point after the first time the user presses enter, (not any more than that,) and I can't even backspace it.

- (void)textViewDidChange:(UITextView *)textView
{
    if ([myTextField.text isEqualToString:@"\n"]) {
        NSString *bullet = @"\u2022";
        myTextField.text = [myTextField.text stringByAppendingString:bullet];
    }
}

如果您只找到一种使用Swift进行执行的方法,请随时发布Swift版本的代码.

If you only find a way performing it with Swift then feel free to post a Swift version of the code.

推荐答案

问题是您正在使用

if ([myTextField.text isEqualToString:@"\n"]) {

作为您的条件,因此如果您的整个 myTextField.text 等于"\ n",则执行该块.但是,如果您没有输入"\ n",则整个 myTextField.text 都只等于"\ n".这就是为什么现在此代码仅在用户首次按下Enter时"起作用的原因;并且当您说我什至不能退格"时,问题实际上是由于调用了 textViewDidChange:而对项目符号点进行了重新添加,因为相同的条件仍在满足中.

as your conditional, so the block executes if the entirety of your myTextField.text equals "\n". But the entirety of your myTextField.text only equals "\n" if you haven't entered anything but "\n". That's why right now, this code is only working "the first time the user presses enter"; and when you say "I can't even backspace it," the problem is actually that the bullet point's being re-added with the call to textViewDidChange: since the same conditional is still being met.

在这种情况下,我建议使用 shouldChangeTextInRange:而不是使用 textViewDidChange:,这样您就可以知道替换文本是什么,无论它在 UITextView 文本字符串.通过使用此方法,即使在文本块的中间输入换行符,也可以自动插入项目符号点.例如,如果用户决定输入一堆信息,则跳回几行要输入更多信息,然后尝试在两者之间按换行符,则下面的内容仍然有效.这是我的建议:

Instead of using textViewDidChange: I recommend using shouldChangeTextInRange: in this case so you can know what that replacement text is no matter it's position within the UITextView text string. By using this method, you can automatically insert the bullet point even when the newline is entered in the middle of the block of text... For example, if the user decides to enter a bunch of info, then jump back up a few lines to enter some more info, then tries to press newline in between, the following should still work. Here's what I recommend:

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

    // If the replacement text is "\n" and the
    // text view is the one you want bullet points
    // for
    if ([text isEqualToString:@"\n"]) {

        // If the replacement text is being added to the end of the
        // text view, 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 bullet point to the end
            NSString *updatedText = [textView.text stringByAppendingString:@"\n\u2022 "];
            [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 bullet point
            // at the point at which the user inputted just the
            // newline character
            [textView replaceRange:textRange withText:@"\n\u2022 "];

            // Update the cursor position accordingly
            NSRange cursor = NSMakeRange(range.location + @"\n\u2022 ".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;
}

这篇关于如何在UITextView中的每个新行中添加无序列表或项目符号点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆