Vertical text in UITextView attributed text

188 Views Asked by At

I have a UITextView in which the user enters text. I've added some code so that when the entered text includes a #hashtag that word would be coloured blue. Here is the code:

- (void)textViewDidChange:(UITextView *)textView
{
    //Coloring of hashtags
    NSArray *words = [textView.text componentsSeparatedByString:@" "];

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textView.text];

    for (NSString *word in words)
    {
        if ([word hasPrefix:@"#"])
        {
            NSRange matchRange = [textView.text rangeOfString:word];
            [attrString addAttribute:NSForegroundColorAttributeName value:[AppereanceConfiguration defaultTintColor] range:matchRange];
        }
    }
    textView.attributedText = attrString;
}

On iOS7 it works fine, but on iOS6 as soon as I start typing the text starts going vertically down. For example: if I wanted to enter the word "Test" - when I press "T" it appears fine, but when I press "e" it appears bellow the "T" in a new line and so on. Every next letter appears under the previous one.

How do I fix this?

1

There are 1 best solutions below

0
On

Try this:

- (void)textViewDidChange:(UITextView *)textView {

[textView setTypingAttributes:{@NSForegroundColorAttributeName:[AppereanceConfiguration defaultTintColor]}];
}

add conditions and don't need to create each time a NSMutableAttributedString.