HTML 到纯文本但不删除超链接

HTML to plain text but not to remove hyperlinks

我从网络服务收到了 HTML 代码。我需要 trim 将其转换为简单文本。但我需要显示 html 代码所具有的链接。可能吗? 这是我的 html 代码。

<div style="text-align: justify;"><img alt="Rangam" src="http://onboarding.rangam.com/Mybase/GetOrganizationLogo/1" style="height:80px; width:263px" /><br />

<p style="text-align:justify">Established in 1995, with offices in three continents, <a href="https://www.rangam.com" target="_blank">Rangam Consultants Inc.</a> is a high-performing diverse supplier of enterprise-wide staffing, payroll and on-boarding services. We are a certified WMBE that has consistently grown year-over-year and have an excellent history of client retention. We are proud that our clients consistently rate us among their top 5 service providers.</p>

<p style="text-align:justify">An expert workforce and cutting-edge technology solutions allow <a href="https://www.rangam.com" target="_blank">Rangam</a> to serve large clients nationwide. Our mature business processes have enabled us to successfully serve Fortune 500 corporations and the public sector.</p>

如果我保持 html 文本不变并在 UITextview 中添加为属性文本,它将看起来像这样。我不需要。我只需要显示带有超链接的文本(图片 2)。

创建示例:

NSString *htmlStr = @"<div style=\"text-align: justify;\"><img alt=\"Rangam\" src=\"http://onboarding.rangam.com/Mybase/GetOrganizationLogo/1\" style=\"height:80px; width:263px\" /><br />\
<p style=\"text-align:justify\">Established in 1995, with offices in three continents, <a href=\"https://www.rangam.com\" target=\"_blank\">Rangam Consultants Inc.</a> is a high-performing diverse supplier of enterprise-wide staffing, payroll and on-boarding services. We are a certified WMBE that has consistently grown year-over-year and have an excellent history of client retention. We are proud that our clients consistently rate us among their top 5 service providers.</p>\
<p style=\"text-align:justify\">An expert workforce and cutting-edge technology solutions allow <a href=\"https://www.rangam.com\" target=\"_blank\">Rangam</a> to serve large clients nationwide. Our mature business processes have enabled us to successfully serve Fortune 500 corporations and the public sector.</p>";

NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUTF8StringEncoding]
                                                                          options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                                               documentAttributes:nil
                                                                            error:nil];

我们在这里做工作:

//We enumerate the attributes and we keep only the ones for NSLinkAttributeName
[attr enumerateAttributesInRange:NSMakeRange(0, [attr length]) options:0 usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
    NSMutableDictionary *cleanDict = [[NSMutableDictionary alloc] init];
    if ([attrs objectForKey:NSLinkAttributeName]) //There is a link
    {
        [cleanDict setObject:[attrs objectForKey:NSLinkAttributeName] forKey:NSLinkAttributeName];
        //You may want to add effects like underline
        if ([attrs objectForKey:NSUnderlineColorAttributeName])
            [cleanDict setObject:[attrs objectForKey:NSUnderlineColorAttributeName] forKey:NSUnderlineColorAttributeName];
        if ([attrs objectForKey:NSUnderlineStyleAttributeName])
            [cleanDict setObject:[attrs objectForKey:NSUnderlineStyleAttributeName] forKey:NSUnderlineStyleAttributeName];
    }
    //We replace the whole attributes with the one we kept (either no attributes, or just the link ones
    [attr setAttributes:cleanDict range:range];
}];

//We can also change the font if we want
[attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, [attr length])];

结果: 第一个是刚创建attr后直接取的,另一个是修改后直接取的。我将字体设置为粗体以便更好地查看更改。