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

时间:2019-11-08 12:38:15

标签: objective-c uitextview nsattributedstring

我在应用中使用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 :(得分:0)

通过将输入属性设置为textView来解决此问题

after 30000 set stop_wait &
vwait stop_wait
unset stop_wait