Search code examples
iosobjective-cuitextviewnsattributedstring

iOS - Editing text in UITextView changes attributes


I am using UITextView to display attributed string data. Everything is working fine but if i edit UITextView in a way like:

Scenario 1: I have two lines first line is with normal font and second line is with bold font if i edit the second line all of the text is converted to Bold.

Scenario 2: If i move the cursor to first line with normal font and remove or add a character all of the bold font is gone.

Scenario 3: If i have a third line with underline font changing it changes all of the text to be underline.

I am using setAllowsEditingTextAttributes as True for my UITextView

Why is this happening? How can i prevent this problem?


Solution

  • use UITextView delegate to set the attribute:

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
        //Here you set the attribute of your text
    }