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

问题描述 投票:0回答:1

我在应用中使用UITextView,但行为异常。我的UITextView具有白色作为文本颜色。我在文本视图中设置了一些带有蓝色的属性文本,它可以按预期工作。但是,一旦我删除了文字,文字颜色就会变成蓝色。我尝试将文本颜色再次设置为白色,但是没有运气。请查看随附的屏幕截图,以更好地了解问题。

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;
}
objective-c uitextview nsattributedstring
1个回答
0
投票

通过将键入属性设置为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;
}
© www.soinside.com 2019 - 2024. All rights reserved.