Цвет текста UITextView не сбрасывается после установки атрибутного текста - PullRequest
0 голосов
/ 08 ноября 2019

Я использую UITextView в своем приложении, и у меня странное поведение. Мой UITextView имеет белый цвет в качестве цвета текста. Я устанавливаю некоторый приписанный текст с синим цветом в моем текстовом представлении, и это работает как ожидалось. Но как только я удаляю текст, цвет текста становится синим. Я попытался установить цвет текста снова на белый, но не повезло. См. Скриншоты для лучшего понимания вопроса.

TextWith attributed and dummy text

Cleared Text

Text turns blue

ОБНОВЛЕНИЕ

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

1 Ответ

0 голосов
/ 09 ноября 2019

Исправлена ​​проблема с установкой атрибутов набора текста в 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;
}
...