美文网首页
TYAttributedLabel 笔记

TYAttributedLabel 笔记

作者: b82e0191c0fe | 来源:发表于2018-08-15 16:09 被阅读166次

    针对项目需求使用TYAttributedLabel,需要修改一下TYAttributedLabel源码

    实现如图效果,部分文字显示背景高亮,并点击时是另外的高亮颜色


    IMG_0167.png

    第一步

    在TYTextContainer.h文件中添加属性

    @property (nonatomic, strong) NSMutableArray *textLinkStorages;//在.m中懒加载textLinkStorages
    

    在TYTextContainer+Extended.m文件中修改

    - (void)appendLinkWithText:(NSString *)linkText linkFont:(UIFont *)linkFont linkColor:(UIColor *)linkColor underLineStyle:(CTUnderlineStyle)underLineStyle linkData:(id)linkData
    {
        TYLinkTextStorage *linkTextStorage = [[TYLinkTextStorage alloc]init];
        linkTextStorage.text = linkText;
        linkTextStorage.font = linkFont;
        linkTextStorage.textColor = linkColor;
        linkTextStorage.linkData = linkData;
        linkTextStorage.underLineStyle = underLineStyle;
        [self appendTextStorage:linkTextStorage];
        [self.textLinkStorages addObject:linkTextStorage];//添加
    }
    
    - (void)addLinkWithLinkData:(id)linkData linkColor:(UIColor *)linkColor underLineStyle:(CTUnderlineStyle)underLineStyle range:(NSRange )range
    {
        TYLinkTextStorage *linkTextStorage = [[TYLinkTextStorage alloc]init];
        linkTextStorage.range = range;
        linkTextStorage.textColor = linkColor;
        linkTextStorage.linkData = linkData;
        linkTextStorage.underLineStyle = underLineStyle;
        [self addTextStorage:linkTextStorage];
        [self.textLinkStorages addObject:linkTextStorage];//添加
    }
    
    
    

    第二步

    在TYAttributedLabel.m中修改

    - (void)drawRect:(CGRect)rect {////}
    

    在代码

    if (_highlightedLinkBackgroundColor && [_textContainer existLinkRectDictionary]) {
            [self drawSelectionAreaFrame:_textContainer.frameRef InRange:_clickLinkRange radius:_highlightedLinkBackgroundRadius bgColor:_highlightedLinkBackgroundColor];
        }
    

    后添加代码

        for (TYLinkTextStorage *storage in _textContainer.textLinkStorages) {
            NSRange storageRange = storage.realRange;
            if (_clickLinkRange.length == 0) {
                [self drawSelectionAreaFrame:_textContainer.frameRef InRange:storageRange radius:_highlightedLinkBackgroundRadius bgColor:[UIColor blueColor]];
            } else {
                if (_clickLinkRange.location != storageRange.location || _clickLinkRange.length != storageRange.length) {
                      [self drawSelectionAreaFrame:_textContainer.frameRef InRange:storageRange radius:_highlightedLinkBackgroundRadius bgColor:[UIColor blueColor]];
                }
            }
        }
    

    修改完成后,调用TYattributedLabel如下代码:

        NSString *testText = @"I'm still very much figuring out what I want to do with my life, but I can honestly say now that I feel very proud of where I am right now. <P>I've written about the teachers in high school and college I had that made a big impact on me, but I haven't mentioned the <b>person</b> that made the biggest difference.";
        NSArray *words = @[@"I'm",@"I",@"person",@"written",@"a",@"I've"];
    
        TYAttributedLabel *textLabel = [[TYAttributedLabel alloc] init];
        textLabel.textColor = [UIColor blackColor];
        textLabel.delegate = self;
        textLabel.font = [UIFont systemFontOfSize:16.0f];
        textLabel.lineBreakMode = kCTLineBreakByWordWrapping;
        [self.view addSubview:textLabel];
        textLabel.frame = CGRectMake(10, 100, self.view.frame.size.width-20, self.view.frame.size.height);
        textLabel.attributedText = [testText dr_HtmlStringWithFontSize:16.0f];
        
        NSString *string = textLabel.text;
        [string enumerateSubstringsInRange:NSMakeRange(0, string.length-1) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
           
            if([words containsObject:substring]) {
                [textLabel addLinkWithLinkData:substring linkColor:[UIColor  colorWithHex:0xffb211] underLineStyle:kCTUnderlineStyleNone range:substringRange];
            }
        }];
    
    

    欢迎大家指教!!!

    相关文章

      网友评论

          本文标题:TYAttributedLabel 笔记

          本文链接:https://www.haomeiwen.com/subject/nhrwbftx.html