HTML转换为纯文本,但不删除超链接

HTML转换为纯文本,但不删除超链接,html,ios,objective-c,nsattributedstring,trim,Html,Ios,Objective C,Nsattributedstring,Trim,我收到了来自webservice的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 />

我收到了来自webservice的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 />

成立于1995年,在三大洲设有办事处,是一家高绩效的多元化供应商,提供企业范围内的人员配备、工资和入职服务。我们是一家经过认证的WMBE,每年都在不断增长,并且拥有优秀的客户保留历史。我们为我们的客户始终将我们评为前五大服务提供商而感到自豪

专家团队和尖端技术解决方案可以为全国范围内的大型客户提供服务。我们成熟的业务流程使我们能够成功地为财富500强企业和公共部门服务

如果我保持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];
NSString*htmlStr=@“
\

成立于1995年,在三大洲设有办事处,是一家高绩效的多元化供应商,提供企业范围内的人员配备、工资和入职服务。我们是一家经过认证的WMBE,每年都在不断增长,并且拥有优秀的客户保留历史。我们为我们的客户始终将我们评为前五大服务提供商而感到自豪。

\

专家团队和尖端技术解决方案可以为全国范围内的大型客户提供服务。我们成熟的业务流程使我们能够成功地为《财富》500强企业和公共部门服务。

“; NSMUTABLeAttributeString*attr=[[NSMUTABLeAttributeString alloc]initWithData:[htmlStr dataUsingEncoding:NSUTF8StringEncoding] 选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} 文件属性:无 错误:无];
我们在这里开展工作:

//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])];
//我们枚举属性,只保留NSLinkAttributeName的属性
[attr enumerateAttributesInRange:NSMakeRange(0,[attr length])选项:0使用块:^(NSDictionary*\u非空属性,NSRange范围,BOOL*\u非空停止){
NSMutableDictionary*cleanDict=[[NSMutableDictionary alloc]init];
if([attrs objectForKey:NSLinkAttributeName])//存在一个链接
{
[cleanDict setObject:[attrs objectForKey:NSLinkAttributeName]forKey:NSLinkAttributeName];
//您可能需要添加下划线等效果
if([attrs objectForKey:NSUnderlineColorAttributeName])
[cleanDict setObject:[attrs objectForKey:NSUnderlineColorAttributeName]forKey:NSUnderlineColorAttributeName];
if([attrs objectForKey:NSUnderlineStyleAttributeName])
[cleanDict setObject:[attrs objectForKey:NSUnderlineStyleAttributeName]forKey:NSUnderlineStyleAttributeName];
}
//我们用保留的属性替换整个属性(要么没有属性,要么只是链接属性)
[attr setAttributes:cleanDict range:range];
}];
//如果需要,我们也可以更改字体
[attr addAttribute:NSFontAttributeName值:[UIFont boldSystemFontOfSize:14]范围:NSMakeRange(0,[attr长度]);
结果: 第一个是在创建
attr
之后直接获取的,另一个是在执行修改之后获取的。我将字体设置为粗体,以便更好地查看更改

创建示例:

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];
NSString*htmlStr=@“
\

成立于1995年,在三大洲设有办事处,是一家高绩效的多元化供应商,提供企业范围内的人员配备、工资和入职服务。我们是一家经过认证的WMBE,逐年增长,拥有优秀的客户保留历史。我们为我们的客户始终将我们评为前五名服务而感到自豪ice供应商。

\

一支专业的员工队伍和尖端技术解决方案可以为全国范围内的大客户提供服务。我们成熟的业务流程使我们能够成功地为财富500强企业和公共部门提供服务。

”; NSMUTABLeAttributeString*attr=[[NSMUTABLeAttributeString alloc]initWithData:[htmlStr dataUsingEncoding:NSUTF8StringEncoding] 选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} 文件属性:无 错误:无];
我们在这里开展工作:

//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])];
//我们枚举属性,只保留NSLinkAttributeName的属性
[attr enumerateAttributesInRange:NSMakeRange(0,[attr length])选项:0使用块:^(NSDictionary*\u非空属性,NSRange范围,BOOL*\u非空停止){
NSMutableDictionary*cleanDict=[[NSMutableDictionary alloc]init];
if([attrs objectForKey:NSLinkAttributeName])//存在一个链接
{
[cleanDict setObject:[attrs objectForKey:NSLinkAttributeName]forKey:NSLinkAttributeName];
//您可能需要添加下划线等效果
if([attrs objectForKey:NSUnderlineColorAttributeName])
[cleanDict setObject:[attrs objectForKey:NSUnderlineColorAttributeName]forKey:NSUnderlineColorAttributeName];
if([attrs objectForKey:NSUnderlineStyleAttributeName])
[cleanDict setObject:[attrs objectForKey:NSUnderlineStyleAttributeName]forKey:NSUnderlineStyleAttributeName];
}
//我们用保留的属性替换整个属性(要么没有属性,要么只是链接属性)
[attr setAtt