iOS中的label加载HTML文件

来源:互联网 发布:linux 图形安装 kvm 编辑:程序博客网 时间:2024/05/01 02:38

 NSString * htmlString =@"<a class='blue' href='http://10.5.225.14:8080/UserCenter/toUserIndex/ecfbdc8c98be410a9ad238511dc16e06.html' target='_blank'>@王琦琦 </a>jksjkdfsdfjsd<a class='blue' href='http://10.5.225.14:8080/UserCenter/toUserIndex/bb4294141cd141b0ba69d788811b7b7d.html' target='_blank'>@leef </a>jfsdlkfjsd<a class='blue' href='http://10.5.225.14:8080/UserCenter/toUserIndex/40c4973fe3fc4d178ffb123e2f765c79.html' target='_blank'>@lee </a>[]";

    NSMutableAttributedString * attrStr = [[NSMutableAttributedStringalloc] initWithData:[htmlStringdataUsingEncoding:NSUnicodeStringEncoding]options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }documentAttributes:nilerror:nil];

    NSLog(@"获取的文本是:%@",attrStr);

    NSError *error;

    NSString *regulaStr =@"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";

    NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:regulaStr

                                                                          options:NSRegularExpressionCaseInsensitive

                                                                            error:&error];

    NSArray *arrayOfAllMatches = [regexmatchesInString:attrStr.stringoptions:0range:NSMakeRange(0, [htmlStringlength])];

    NSLog(@"%@",arrayOfAllMatches);

    for (NSTextCheckingResult *matchin arrayOfAllMatches)

    {

        NSString* substringForMatch = [htmlStringsubstringWithRange:match.range];

         NSLog(@"%@",substringForMatch);

    }

    UILabel * myLabel = [[UILabelalloc] initWithFrame:self.view.bounds];

    myLabel.attributedText = attrStr;

    [self.viewaddSubview:myLabel];

0 0
原创粉丝点击