iOS中的label加载HTML文件

 NSString * htmlString = @"@王琦琦 jksjkdfsdfjsd@leef jfsdlkfjsd@lee []";

    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

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

    NSError *error;

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

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr

                                                                           options:NSRegularExpressionCaseInsensitive

                                                                             error:&error];

    NSArray *arrayOfAllMatches = [regex matchesInString:attrStr.string options:0 range:NSMakeRange(0, [htmlString length])];

    NSLog(@"%@",arrayOfAllMatches);

    for (NSTextCheckingResult *match in arrayOfAllMatches)

    {

        NSString* substringForMatch = [htmlString substringWithRange:match.range];

          NSLog(@"%@",substringForMatch);

    }

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

    myLabel.attributedText = attrStr;

    [self.view addSubview:myLabel];

你可能感兴趣的:(ios)