如何在 UITextView 中添加无序列表或项目符号指向每个新行



所以基本上我想在UITextView中添加一个无序列表。对于另一个UITextView我想添加一个有序列表。

我尝试使用此代码,但它仅在用户第一次按 Enter 后给了我一个项目符号,(仅此而已(,我什至无法退格。

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

如果你只找到一种使用 Swift 执行它的方法,那么请随时发布代码的 Swift 版本。

问题是你正在使用

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

作为您的条件,因此如果整个myTextField.text等于"",则执行块。但是,如果您没有输入除"">以外的任何内容,则整个myTextField.text仅等于""。这就是为什么现在,这段代码只在"用户第一次按 Enter 时"工作;当你说"我什至不能退格键"时,问题实际上是项目符号点被重新添加到textViewDidChange:的调用中,因为仍然满足相同的条件。

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

- (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:@"nu2022 "];
            [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:@"nu2022 "];
            // Update the cursor position accordingly
            NSRange cursor = NSMakeRange(range.location + @"nu2022 ".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;
}

以防万一,有人正在寻找解决同一问题的 Swift 2.x 解决方案,这里有一个解决方案:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    // If the replacement text is "n" and the
    // text view is the one you want bullet points
    // for
    if (text == "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.characters.count {
            // Simply add the newline and bullet point to the end
            var updatedText: String = textView.text!.stringByAppendingString("n u{2022} ")
            textView.text = updatedText
        }
        else {
            // Get the replacement range of the UITextView
            var beginning: UITextPosition = textView.beginningOfDocument
            var start: UITextPosition = textView.positionFromPosition(beginning, offset: range.location)!
            var end: UITextPosition = textView.positionFromPosition(start, offset: range.length)!
            var textRange: UITextRange = 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 u{2022} ")
            // Update the cursor position accordingly
            var cursor: NSRange = NSMakeRange(range.location + "n u{2022} ".length, 0)
            textView.selectedRange = cursor
        }
        return false

    }
    // Else return yes
    return true
}

斯威夫特 5.x 版本。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        //
        // If the replacement text is "n" and the
        // text view is the one you want bullet points
        // for
        if (text == "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.count {
                // Simply add the newline and bullet point to the end
                let updatedText: String = textView.text! + "n u{2022} "
                textView.text = updatedText
            }
            else {
                // Get the replacement range of the UITextView
                let beginning: UITextPosition = textView.beginningOfDocument
                
                let start: UITextPosition = textView.position(from: beginning, offset: range.location)!
                let end: UITextPosition = textView.position(from: start, offset: range.length)!
                
                let textRange: UITextRange = textView.textRange(from: start, to: end)!
                // Insert that newline character *and* a bullet point
                // at the point at which the user inputted just the
                // newline character
                textView.replace(textRange, withText: "n u{2022} ")
                // Update the cursor position accordingly
                let cursor: NSRange = NSMakeRange(range.location + "n u{2022} ".count, 0)
                textView.selectedRange = cursor
            }
            return false

        }
        // Else return yes
        return true
    }

最新更新