设置属性文本后,UITextView 文本颜色不会重置 [英] UITextView text color doesn't reset after setting attributed text
本文介绍了设置属性文本后,UITextView 文本颜色不会重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序中使用 UITextView
并且我有一个奇怪的行为.我的 UITextView
将白色作为文本颜色.我在我的 UITextView
中设置了一些带有蓝色的属性文本,它按预期工作,但是一旦我删除文本并下次输入,文本颜色就会变成蓝色.我尝试再次将文本颜色设置为白色,但没有成功.请参阅随附的屏幕截图以更好地了解问题.
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.
更新
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 解决了该问题
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;
}
这篇关于设置属性文本后,UITextView 文本颜色不会重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文