TYAttributedLabel的使用

端木元青
2023-12-01
#import "TYAttributedLabel.h"
 //提示内容
    lblContent=[[TYAttributedLabel   alloc]init];
    [bgView   addSubview:lblContent];
    lblContent.delegate=self;
    NSString *text = @"    温馨提示\n亲,感谢您对xxxx一直以来的信任!更新了xxxx《aaaa》和《bbbb》,特向您说明如下:\n1、为您xxxxxxxxxxxxx";
    [lblContent setText:text];
    // 文字间隙
    lblContent.characterSpacing = 2;
    // 文本行间隙
    lblContent.linesSpacing = 5;
    TYTextStorage  *  titleStorage = [[TYTextStorage alloc]init];
    titleStorage.range = [text rangeOfString:@"温馨提示"];
    titleStorage.textColor =[UIColor   hx_colorWithHexString:blackFontColor];
    titleStorage.font = [UIFont boldSystemFontOfSize:20];
    [lblContent addTextStorage:titleStorage];
    TYTextStorage  *  textStorage = [[TYTextStorage alloc]init];
    textStorage.range = [text rangeOfString:@"《aaaa》"];
    textStorage.textColor =[UIColor   hx_colorWithHexString:navBarColor];
    textStorage.font = [UIFont boldSystemFontOfSize:14];
    [lblContent addTextStorage:textStorage];
    TYTextStorage  *  privacyStorage = [[TYTextStorage alloc]init];
    privacyStorage.range = [text rangeOfString:@"《bbbb》"];
    privacyStorage.textColor =[UIColor   hx_colorWithHexString:navBarColor];
    privacyStorage.font = [UIFont boldSystemFontOfSize:14];
    [lblContent addTextStorage:privacyStorage];
     //添加超链接
    [lblContent   addLinkWithLinkData:@"aaaa" linkColor:[UIColor   hx_colorWithHexString:navBarColor] underLineStyle:kCTUnderlineStyleNone range:[text rangeOfString:@"《aaaa》"]];
    //添加超链接
    [lblContent   addLinkWithLinkData:@"bbbb" linkColor:[UIColor   hx_colorWithHexString:navBarColor] underLineStyle:kCTUnderlineStyleNone range:[text rangeOfString:@"《bbbb》"]];
    [lblContent sizeToFit];
    CGFloat   height_lblContent=[lblContent  getSizeWithWidth:gScreenWidth-20].height;
    [lblContent   mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(height_lblContent);
        make.bottom.mas_equalTo(-80);
        
    }];


添加协议

<TYAttributedLabelDelegate>

点击超链接

#pragma mark - Delegate
//TYAttributedLabelDelegate
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)TextRun atPoint:(CGPoint)point {
    if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {
        NSString *linkStr = ((TYLinkTextStorage*)TextRun).linkData;
        if ([linkStr   isEqualToString:@"aaaa"]) {
            WYWebController *webVC = [WYWebController new];
            webVC.mTitle = @"aaaa";
            webVC.url =@"aaa";
            webVC.hidesBottomBarWhenPushed=YES;
            [self.nav pushViewController:webVC animated:YES];
        }else{
            
            WYWebController *webVC = [WYWebController new];
            webVC.mTitle = @"bbbb";
            webVC.url =@"bbbb"
            webVC.hidesBottomBarWhenPushed=YES;
            [self.nav pushViewController:webVC animated:YES];
            
        }
    }
}
 类似资料: