Horray Horray - 2 years ago 459
Swift Question

How do I add an unordered list or a bullet point every new line in a UITextView

So basically I want to add an unordered list to a

. And to another
I want to add an ordered list.

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.

- (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.

Answer Source

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-added with the call to textViewDidChange: since the same conditional is still being met.

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:

- (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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download