7

I am creating an app in which i have to implement functionality like this:

1) Write into textview

2) Select text from textview

3) Allow user to apply bold,italic and underline functionality on selected text.

I have started implementing it using NSMutableAttributedString. It's working for bold and italic but replaces the textview text with only selected text.

-(void) textViewDidChangeSelection:(UITextView *)textView
{
       rangeTxt = textView.selectedRange;
       selectedTxt = [textView textInRange:textView.selectedTextRange];
       NSLog(@"selectedText: %@", selectedTxt);

}

-(IBAction)btnBold:(id)sender
{

    UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];

    NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedTxt attributes:boldAttr];

    txtNote.attributedText = attributedText;

}

Can anybody please help me out to implement this functionality?

Thanks in advance.

Shah Paneri
  • 729
  • 7
  • 28
  • 1
    This is built into `UITextView` (as of iOS 6.0). Set the `allowsEditingTextAttributes` property to `YES`. – rmaddy Jul 10 '13 at 05:32
  • I have already done this. My problem is only the selected text with bold/italic is remaining in textview. Other text are removed. I want to replace only selected text with bold/italic in textview text. – Shah Paneri Jul 10 '13 at 05:38
  • 3
    By enabling the `allowsEditingTextAttributes` property you don't need any of the code you posted. If you select some text in the text view, the text view automatically offers the BUI (bold/italic/underline) menu option. You don't need to write any code at all for this. – rmaddy Jul 10 '13 at 05:40
  • No. it's not allowing me. I have written `txtNote.allowsEditingTextAttributes = YES;` in viewDidLoad method. But it's same as before. – Shah Paneri Jul 10 '13 at 05:46
  • What do you mean by "same as before"? This works for me. Set that property and I see the new formatting menu when selecting text in the text view. Make sure `txtNote` isn't `nil` when you try to set the property. – rmaddy Jul 10 '13 at 05:49
  • Yes.It's displaying option bold,italic and underline on selection of text. Thank you so much. :) – Shah Paneri Jul 10 '13 at 05:55

1 Answers1

1

You should not use didChangeSelection for this purpose. Use shouldChangeTextInRange instead.

This is because when you set the attributed string to new one you don't replace the text of certain location. You replace full text with your new text. You need range to locate the position where you want the text changed.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

     NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];

    NSRange selectedTextRange = [textView selectedRange];
    NSString *selectedString = [textView textInRange:textView.selectedTextRange];

    //lets say you always want to make selected text bold
    UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];

    NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedString attributes:boldAttr];

   // txtNote.attributedText = attributedText; //don't do this

    [textViewText replaceCharactersInRange:range withAttributedString:attributedText]; // do this

    textView.attributedText = textViewText;
    return false;
}
Jawa
  • 2,336
  • 6
  • 34
  • 39
Sahar
  • 53
  • 7