我有从网络服务收到的 HTML 代码。我需要将其修剪为简单的文本。但我需要显示 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
创建后直接取的,另一个是修改后直接取的。我将字体设置为粗体,以便更好地查看更改。
关于HTML 到纯文本但不删除超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887885/