-3
   - (void)textViewDidChangeSelection:(UITextView *)textView
{
        NSRange range = [bodyField selectedRange];
        NSString *str = [bodyField.text substringWithRange:range];

}

At this point, I would implement the change of type when the selection, from normal, but I can't continue.

Edit: thanks to @danh:

The only thing that doesn't work is that if I select the text is already Bold, not back to normal. How do I fix?

Thank you guys :)

Ōmega
  • 39
  • 6
  • Please refer [http://stackoverflow.com/questions/27997107/how-to-bold-some-words-in-my-uitextview-using-nsmutableattributedstring] Thanks Sriram – Sri Jan 28 '17 at 14:55
  • I've seen, it doesn't work :( – Ōmega Jan 28 '17 at 14:56
  • "No visible @interface for 'UITextView' declares the selector 'rangeOfString:'" – Ōmega Jan 28 '17 at 15:07
  • Possible duplicate of [How to bold some words in my UITextView using NSMutableAttributedString?](http://stackoverflow.com/questions/27997107/how-to-bold-some-words-in-my-uitextview-using-nsmutableattributedstring) – Ōmega Jan 28 '17 at 17:21

1 Answers1

0

It's a mistake to change a text view's text in the selectionChanged delegate method because that method is called repeatedly for intermediate steps in the selection, and it's called again after the text is modified.

Modifying the text attributes in a selected range is easily done while the selection is held still. For example, if have add a button, or some other event to trigger the change...

// assuming you have an outlet to the text view called textView.
// assuming you have a button set to trigger this action.
- (IBAction)doit:(id)sender {

    // this way, the old attributed state will be removed and replaced
    // NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.textView.text];

    // this way, we'll add attributes to the existing attributes
    NSMutableAttributedString *attributedString = [self.textView.attributedText mutableCopy];

    UIFont *font = [UIFont boldSystemFontOfSize:17];
    NSRange selectedRange = [self.textView selectedRange];

    [attributedString setAttributes:@{ NSFontAttributeName: font } range:selectedRange];
    [self.textView setAttributedText:attributedString];
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • Oh..It is difficult to select a portion of text and make it bold? :O – Ōmega Jan 28 '17 at 15:20
  • Not difficult. But you must start off with some text. The error you first encountered indicates that you were trying manipulate the text view itself, rather than the text it contains. – danh Jan 28 '17 at 15:23
  • I can't figure out how to make the selected text bold. Selecting text that interests me works, but the transformation in bold no! – Ōmega Jan 28 '17 at 15:31
  • Happy to help further, but I can't unless I look at your code. – danh Jan 28 '17 at 15:34
  • `- (void)textViewDidChangeSelection:(UITextView *)textView { NSRange range = [bodyField selectedRange]; NSString *str = [bodyField.text substringWithRange:range]; } @end` – Ōmega Jan 28 '17 at 15:42
  • This is the piece of code that ' captures ' the selected text. And actually the selection works (I tried to print the selected text with a NSLog). In this same method I would implement the change of text, from normal to bold, but I do not work no example found online – Ōmega Jan 28 '17 at 15:42
  • Please edit the question, adding the code that gets the selection, creates the bold text, and assigns it to the text view. – danh Jan 28 '17 at 15:44
  • Answer changed. The key thing to understand is that the UI needs **some other triggering event** besides the selection changing. Some other user action, like a button called "make it bold" would make sense both technically and for UI style – danh Jan 28 '17 at 16:10
  • i have a problem, the button works perfectly. If not then I select anything and I click the button, the text gets smaller – Ōmega Jan 28 '17 at 16:29
  • Do you mean you want the bold part to increase, to add more bold text to the previous selection? If so, see edit. – danh Jan 28 '17 at 16:34