富文本常用方法

高亮点击

- (NSAttributedString *)moreTipsAttFunction {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"500_published_about_check"]];
    imageView.frame = CGRectMake(0, 0, 18, 18);
    NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:[MouoFont PingFangSC_RegularWithSize:14] alignment:YYTextVerticalAlignmentCenter];
    
    NSString *str1 = @"我已阅读并同意";
    NSString *str2 = @"《陌友钱包用户协议》";
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",str1,str2]];
    att.yy_color = [UIColor colorWithHexString:@"#9B9B9B"];
    [att yy_setColor:[UIColor colorWithHexString:@"#2296F3"] range:NSMakeRange(str1.length, str2.length)];
    
    //高亮
    YYTextHighlight *protocolHightLight = [YYTextHighlight new];
    protocolHightLight.userInfo = @{@"title":@"tap"};
    [att yy_setTextHighlight:protocolHightLight range:NSMakeRange(str1.length, str2.length)];
    att.yy_font = [MouoFont PingFangSC_RegularWithSize:14];
    [att insertAttributedString:attachText atIndex:0];
    
    return att;
}

点击回调

    [self.moreLabel setHighlightLongPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        YYTextHighlight *highlight = [text yy_attribute:YYTextHighlightAttributeName atIndex:range.location];
        NSString *ext = highlight.userInfo[@"title"];
        if ([ext isEqualToString:@"tap"]) {
            //处理点击事件
            NSLog(@"处理点击事件");
        }
    }];

你可能感兴趣的:(富文本常用方法)