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

标签 html ios objective-c nsattributedstring trim

我有从网络服务收到的 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)。

enter image description here

enter image description here

最佳答案

创建示例:

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创建后直接取的,另一个是修改后直接取的。我将字体设置为粗体,以便更好地查看更改。

enter image description here

关于HTML 到纯文本但不删除超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887885/

相关文章:

html - Angularjs Contenteditable 在 ng-repeat 中与行/列 Action

ios - 在 iOS 模拟器中测试 SMS

objective-c - 修改 NSArray 中 UILabels 的文本属性

ios - AVAudioPlayer 被释放,而键值观察者仍然在其中注册

iphone - sqlite 准备语句错误-没有这样的表

javascript - 如果 X 与 Y 的 href 匹配,则添加类 'active' - jQuery

javascript - JavaScript 中的换行符?

html - 创建网站模板。两个独立设计的间距不同,但应该相同

iphone - 如何在具有正确图像大小和滚动的 UIScrollView 内将 UIImageView 旋转 90 度?

android - 无论屏幕大小如何,如何将元素与背景对齐?