coretext 聚合

- (void)formatTextInTextView:(UITextView *)textView { textView.scrollEnabled = NO; NSRange selectedRange = textView.selectedRange; NSString *text = textView.text; // This will give me an attributedString with the base text-style NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text]; NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error]; NSArray *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)]; for (NSTextCheckingResult *match in matches) { NSRange matchRange = [match rangeAtIndex:0]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:matchRange]; } textView.attributedText = attributedString; textView.selectedRange = selectedRange; textView.scrollEnabled = YES; }
 
///////////////////////////////////////////////////////////
 
 

你可能感兴趣的:(coretext 聚合)