设置属性文本后,UITextView 文本颜色不会重置

UITextView text color doesn't reset after setting attributed text

我在我的应用程序中使用 UITextView,但我有一个奇怪的行为。我的 UITextView 文本颜色为白色。我在我的 UITextView 中设置了一些蓝色的属性文本,它按预期工作,但是一旦我删除文本并在下次输入时,文本颜色就会变成蓝色。我尝试再次将文本颜色设置为白色,但没有成功。请参阅随附的屏幕截图以更好地了解问题。

更新

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;
}

通过将键入属性设置为 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;
}