Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITextView中的每一行添加无序列表或项目符号_Ios_Objective C_Xcode_Swift - Fatal编程技术网

Ios 如何在UITextView中的每一行添加无序列表或项目符号

Ios 如何在UITextView中的每一行添加无序列表或项目符号,ios,objective-c,xcode,swift,Ios,Objective C,Xcode,Swift,因此,基本上我想向UITextView添加一个无序列表。在另一个UITextView中,我想添加一个有序列表 我试着使用这段代码,但在用户第一次按enter键后,它只给了我一个要点,(没有更多),我甚至不能退格 - (void)textViewDidChange:(UITextView *)textView { if ([myTextField.text isEqualToString:@"\n"]) { NSString *bullet = @"\u2022";

因此,基本上我想向
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
的整个值等于“\n”,则会执行块。但是如果除了“\n”之外没有输入任何内容,则整个
myTextField.text
仅等于“\n”。这就是为什么现在,这个代码只在“用户第一次按enter”时才起作用;当你说“我甚至不能退格”的时候,问题实际上是,由于仍然满足相同的条件,所以在调用
textViewDidChange:
时,要点被重新添加了

在这种情况下,我建议不要使用
textViewDidChange:
而是使用
shouldChangeTextInRange:
,这样无论替换文本在
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:@"\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;
}

为了以防万一,任何人都在寻找相同问题的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
}
Swift 5.x版本


(仍在对我的答案进行一些更新,以说明中间文本视图条目。)我只有一个问题:在if语句中添加“text”而不是myText.text有什么意义。我试着把myText.text放进去,但没用,只对“text”起作用。所以我的问题是,有什么区别吗?@Mike,没问题。。。但是,是的,因为我在你发布的同一时间输入,我想做一些更新来处理中间文本视图条目和结束文本视图条目。这没什么大不了的,也没什么区别,但只会让答案更准确一点…@Mike Haha我们有在同一时间发布的诀窍,嗯?;)
myText.text
是文本字段中的文本,而
text
只是
replacementText
,在这种情况下,如委托方法参数所示,即“\n”。我没有得到中间文本视图和末端文本视图之间的差异签出此答案(对于Swift)签出此答案(对于Swift)这工作得很好。必须对Swift 4语法进行一些小的更改。positionFromPosition已重命名为position(from:to:),textRangeFromPosition已重命名为textRange(from:to:),如何将其应用于SwiftUI中的
TextEditor
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
    }