How to add unordered list or bullet to every new line in UITextView
So basically I want to add an unordered list to UITextView
. And to another UITextView
I want to add an ordered list.
I tried using this code, but it only gave me a marker after the user first pressed input (nothing more) and I can't even defer it.
- (void)textViewDidChange:(UITextView *)textView
{
if ([myTextField.text isEqualToString:@"\n"]) {
NSString *bullet = @"\u2022";
myTextField.text = [myTextField.text stringByAppendingString:bullet];
}
}
If you just find a way to accomplish it with Swift then feel free to post the Swift version of the code.
source to share
The problem is that you are using
if ([myTextField.text isEqualToString:@"\n"]) {
as your conditional, so the block is executed if all of yours myTextField.text
is "\ n". But all yours myTextField.text
is "\ n" if you didn't enter anything other than "\ n". This is why right now this code only works "on first user click"; and when you say "I can't even postpone it", the problem is that the bullet is added again with a call to textViewDidChange:
, since the corresponding condition is still met.
Instead of using, textViewDidChange:
I recommend using shouldChangeTextInRange:
in this case so that you can know that this replacement text does not matter in the text string UITextView
. Using this method, you can automatically insert a bullet point even when a new line is entered in the middle of a block of text ... For example, if the user decides to enter a bunch of information, then jump a few lines to enter additional information, then tries to hit a new line in between, the following should 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 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 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;
}
source to share
Just in case anyone was looking for a Swift 2.x solution for the same problem, here is the solution:
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 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
}
source to share