xcode 如何在 UITextView 中的每个新行中添加无序列表或项目符号点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27304655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add an unordered list or a bullet point every new line in a UITextView
提问by Horray
So basically I want to add an unordered list to a UITextView
. And to another UITextView
I want to add an ordered list.
所以基本上我想将一个无序列表添加到UITextView
. 另一个UITextView
我想添加一个有序列表。
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.
我尝试使用此代码,但在用户第一次按下 Enter 后,它只给了我一个要点,(仅此而已),我什至无法退格它。
- (void)textViewDidChange:(UITextView *)textView
{
if ([myTextField.text isEqualToString:@"\n"]) {
NSString *bullet = @"\u2022";
myTextField.text = [myTextField.text stringByAppendingString:bullet];
}
}
If you only find a way performing it with Swift then feel free to post a Swift version of the code.
如果您只找到一种使用 Swift 执行它的方法,那么请随时发布代码的 Swift 版本。
回答by Lyndsey Scott
The problem is that you're using
问题是你正在使用
if ([myTextField.text isEqualToString:@"\n"]) {
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-addedwith the call to textViewDidChange:
since the same conditional is still being met.
作为你的条件,所以如果你的整体myTextField.text
等于“\n” ,块就会执行。但是,myTextField.text
如果您除了“\n”之外没有输入任何内容,则您的only的整体等于“\n”。这就是为什么现在,这段代码只在“用户第一次按下回车键时”起作用;当你说“我什至不能退格它”时,问题实际上是子弹点被重新添加到调用中,textViewDidChange:
因为仍然满足相同的条件。
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:
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;
}
回答by KuboAndTwoStrings
Just in case, anyone was looking for a Swift 2.x solution to the same problem, here's a solution:
以防万一,有人正在寻找针对同一问题的 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
}