如何在UITextView中添加无序列表或项目符号

所以基本上我想添加一个无序的列表到UITextView 。 和另一个UITextView我想添加一个有序列表。

我尝试使用这个代码,但它只给了我第一次用户按下input后,(没有任何比这更多),我甚至不能退格。

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

如果你只是find一种方式与Swift执行,然后随意张贴Swift版本的代码。

问题是你正在使用

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

作为您的条件,所以如果您的myTextField.text的整体等于“\ n”,则该块将执行。 但是,如果您没有input任何内容,而您的myTextField.text的全部内容只与“\ n”相等, “\ n”。 这就是为什么现在这个代码只在“第一次用户按下input”时才起作用。 而当你说“我什至不能退格”时,问题实际上就是这个子弹点被重新添加了对textViewDidChange:的调用textViewDidChange:因为相同的条件仍然被满足。

而不是使用shouldChangeTextInRange: textViewDidChange:我推荐使用shouldChangeTextInRange:在这种情况下,所以你可以知道replace文本是什么,不pipe它在UITextView文本string中的位置。 通过使用此方法,即使在文本块的中间input换行符时,也可以自动插入项目符号点…例如,如果用户决定input一堆信息,则跳回几行进入一些更多的信息,然后试图按下换行符之间,下面应该仍然工作。 以下是我的build议:

 - (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, ie 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解决同样的问题,这里有一个解决scheme:

 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, ie 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 }