iOS部分文字高亮并可以点击

引入第三方框架TYAttributedLabel https://github.com/12207480/TYAttributedLabel

文字控件的布局

    TYAttributedLabel *textLbl=[[TYAttributedLabel alloc]init];
    textLbl.textColor=[UIColor blackColor];
    textLbl.delegate=self;
    textLbl.font=MinLabelFont;
    textLbl.numberOfLines=0;
    textLbl.text=@"阅读并接受";

    
    [self addSubview:textLbl];
    [textLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.selectBtn.mas_top);
        make.left.equalTo(weakSelf.selectBtn.mas_right).with.mas_offset(5);
        make.right.equalTo(weakSelf.mas_right).with.mas_offset(-10);
        make.height.equalTo(@(40));
    }];
    
    [textLbl appendLinkWithText:@"《服务协议》" linkFont:MinLabelFont linkColor:[UIColor colorWithhexString:@"#ffb211"] underLineStyle:kCTUnderlineStyleNone linkData:@"http://baidu.com"];
    
    [textLbl appendText:@"、"];
    
    [textLbl appendLinkWithText:@"《须知》" linkFont:MinLabelFont linkColor:[UIColor colorWithhexString:@"#ffb211"] underLineStyle:kCTUnderlineStyleNone linkData:@"http://baidu.com"];
        [textLbl appendText:@"、"];
        [textLbl appendLinkWithText:@"《服务者管理规则》" linkFont:MinLabelFont linkColor:[UIColor colorWithhexString:@"#ffb211"] underLineStyle:kCTUnderlineStyleNone linkData:@"http://baidu.com"];

点击对应的文字会调用相关的代理的方法

#pragma mark - Delegate
//TYAttributedLabelDelegate
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id)TextRun atPoint:(CGPoint)point {
    if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {
        NSString *linkStr = ((TYLinkTextStorage*)TextRun).linkData;
        NSLog(@"linkStr === %@",linkStr);
        
        KnowWebViewController *knowVc=[[KnowWebViewController alloc]init];
        knowVc.url=linkStr;
        [self.viewController.navigationController pushViewController:knowVc animated:YES];
    }
}

你可能感兴趣的:(iOS部分文字高亮并可以点击)