iOS - UITextView 匹配@和#号的特殊颜色

1、颜色关键类

 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];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值