Search code examples
objective-cuitextviewnsattributedstring

UITextView text color doesn't reset after setting attributed text


I am using UITextView in my app and I'm having a strange behaviour. My UITextView has white colour as text colour. I am setting some attributed text with blue colour in my UITextView and it works as expected, But as soon as I delete the text and type next time, text colour turns blue. I have tried setting text colour again to white but no luck. See screenshots attached for better understanding of the issue.

TextWith attributed and dummy text

Cleared Text

Text turns blue

UPDATE

1. code to turn particular text to blue

      -(NSMutableAttributedString*)decorateTagsWithString:(NSString *)string andTaggedUsers:(NSArray*)taggedUsers {


    NSError *error = nil;

    if (!string) {
        return nil;
    }


    //NSRegularExpression *regexHash = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];


    //NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(@|#)(\\w+)" options:0 error:&error];


    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:16.0f] ,NSForegroundColorAttributeName: [UIColor whiteColor]};
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:attrs];

    if (taggedUsers.count > 0) {

        for (AtTagUserModel* selectedFriend in taggedUsers) {
            NSString *pattern = [NSString stringWithFormat:@"@%@", [self decodeToUTF:selectedFriend.alias]];

            NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
            NSArray *matches = [expression matchesInString:string options:0 range:NSMakeRange(0, string.length)];

            NSInteger stringLength = [string length];

            for (NSTextCheckingResult *match in matches) {

                NSRange wordRange = [match rangeAtIndex:0];

                NSString* word = [string substringWithRange:wordRange];

                //Set Font
                UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:15.0f];
                [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, stringLength)];


                //Set Background Color
                //UIColor *backgroundColor = [UIColor orangeColor];
                //[attString addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:wordRange];

                //Set Foreground Color
                UIColor *foregroundColor = kTagColor;
                [attString addAttribute:NSForegroundColorAttributeName value:foregroundColor range:wordRange];

                NSString* userId = [NSString stringWithFormat:@"%@%@",@"875698789456",selectedFriend.userId];
                NSURL* url = [NSURL URLWithString:userId];

                [attString addAttribute:NSLinkAttributeName value:url range:wordRange];
                // NSLog(@"Found tag %@", word);

            }
        }
    }
    return attString;
}


- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    NSString* fullText = [growingTextView.text stringByReplacingCharactersInRange:range withString:text];


    if (fullText.length == 0 && self.isReplying && self.selectedComment) {
        self.grTextView.internalTextView.attributedText = [[NSAttributedString alloc] initWithString:fullText attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:15]}];
//        self.grTextView.internalTextView.attributedText = nil;
        self.grTextView.placeholderColor = [UIColor whiteColor];
        self.grTextView.textColor = [UIColor whiteColor];
        self.grTextView.font = [UIFont systemFontOfSize:15.0f];
    }



    return YES;
}

Solution

  • Fixed the issue by setting typing attributes to textView

    - (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
        NSString* fullText = [growingTextView.text stringByReplacingCharactersInRange:range withString:text];
        growingTextView.internalTextView.typingAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]};
    
        return YES;
    }