iOS Html Parser:获取网址

时间:2016-04-02 08:37:58

标签: html ios objective-c

我有一个字符串,我使用NSMutableAttributedString转换为NSUTF8StringEncoding

 NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithData:[receivedText dataUsingEncoding:NSUTF8StringEncoding]
                                                                              options:@{
                                                                                        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                        NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                                   documentAttributes:nil error:nil];

html解析按预期工作。但我的字符串包含一些需要可点击的url链接。在html解析之后,我的url字符串将是蓝色并加下划线,但不是可点击的。解析后,我得到NSLink字段,其中包含url。我不确定如何获取网址并通过将其设为NSLinkAttributeName来使其可以点击。请帮我。解析后我的字符串的一部分是:

google.com{
    NSBackgroundColor = "UIDeviceRGBColorSpace 1 1 1 1";
    NSColor = "UIDeviceRGBColorSpace 0.537255 0.537255 0.537255 1";
    NSFont = "<UICTFont: 0x7fdacb166a40> font-family: \".SFUIText-Regular\"; font-weight: normal; font-style: normal; font-size: 15.70pt";
    NSKern = 0;
    NSLink = "http://www.google.com/";
    NSParagraphStyle = "Alignment 3, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0.537255 0.537255 0.537255 1";
    NSStrokeWidth = 0;
    NSUnderline = 1;
}

0 个答案:

没有答案