我的要求是,在运行时在UITextView中键入时,我需要用红色突出显示字符"#"。我知道NSAttributedString和在"shouldChangeTextInRange"中编写逻辑是可能的。我不擅长NSAttributedSString的概念。
求你了,有人能帮我逻辑一下吗。
UITextView有一个属性"typeingAttributes",允许您在需要时更改属性。如果需要,您可以通过使用bool来提高以下代码的内存效率。
- (void)viewDidLoad
{
[super viewDidLoad];
normalAttrdict = [NSDictionary dictionaryWithObject:[UIColor brownColor] forKey:NSForegroundColorAttributeName];
highlightAttrdict = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"#"]) {
textView.typingAttributes = highlightAttrdict;
}
else{
textView.typingAttributes = normalAttrdict;
}
return YES;
}