Objective C,将 html 转换为 NSAttributedString 并禁用超链接

Objective C, Convert html to NSAttributeString and disable the hyperlinks

我正在使用

    [[NSAttributedString alloc] initWithData:[HTMLText                         
    dataUsingEncoding:NSUTF8StringEncoding] options:options 
    documentAttributes:NULL error:NULL];  

将 HTML 字符串转换为 AttributeString 并在 textView 中使用它,但是,我不希望文本中的超链接在 textView 中可点击,有什么方法可以删除超链接文字背后?

NS(Mutable)AttributedString 中的 link 由属性 NSAccessibilityLinkTextAttribute 表示。您可以删除单个属性。大纲:

  • 制作属性字符串的可变副本
  • 使用enumerateAttribute:inRange:options:usingBlock:定位所有links
  • 在传递给上面的块中使用 removeAttribute:range: 删除 link

阅读 Apple 的 NSAtributedString documentation。 HTH