FPTextStorage.h类:
#import <UIKit/UIKit.h>
@interface FPTextStorage : NSTextStorage
- (NSString *)string;
- (NSDictionary *)attributesAtIndex:(NSUInteger)location
effectiveRange:(NSRangePointer)range;
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
@end
FPTextStorage.m类:
#import "FPTextStorage.h"
@implementation FPTextStorage
{
NSMutableAttributedString *_imp;
}
- (id)init
{
self = [super init];
if (self) {
_imp = [NSMutableAttributedString new];
}
return self;
}
- (NSString *)string
{
return _imp.string;
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
return [_imp attributesAtIndex:location effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
[_imp replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters range:range
changeInLength:(NSInteger)str.length - (NSInteger)range.length];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
[_imp setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}
- (void)processEditing
{
[super processEditing];
static NSRegularExpression *iExpression;
NSString *pattern = @"(@[\\S]{0,28} |#[\\S]{0,28}#)";
iExpression = iExpression ?: [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
NSRange paragaphRange = [self.string paragraphRangeForRange: self.editedRange];
[self removeAttribute:NSForegroundColorAttributeName range:paragaphRange];
[iExpression enumerateMatchesInString:self.string
options:0 range:paragaphRange
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
[self addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range];
}];
static NSRegularExpression *isEndExpression;
NSString *endPattern = @"(#[\\S]{0,28}#$|#[\\S]{0,28}#\\S)";
isEndExpression = isEndExpression ?: [NSRegularExpression regularExpressionWithPattern:endPattern options:0 error:NULL];
NSRange endRange = [self.string paragraphRangeForRange:self.editedRange];
[self removeAttribute:NSBackgroundColorAttributeName range:endRange];
[isEndExpression enumerateMatchesInString:self.string options:0 range:endRange usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
if (result.range.location + result.range.length < self.string.length) {
[self addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(result.range.location, result.range.length - 1)];
}else{
[self addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:result.range];
}
}];
}
使用方法:
FPTextStorage *textStorage = [[FPTextStorage alloc] init];
NSLayoutManager *layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager: layoutManager];
NSTextContainer *textContainer = [NSTextContainer new];
textContainer.lineBreakMode = NSLineBreakByCharWrapping;
textContainer.widthTracksTextView = YES;
[layoutManager addTextContainer: textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 64, 280, 300) textContainer:textContainer];
textView.font=[UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;
textView.text = @"哈哈哈#hha哈哈# ggg@gajkga jkjghjg#tt#";//设置显示的文本内容
textView.delegate = self;
[self.view addSubview:textView];