如何解析html标记网址并使其在标签中可点击

问题描述 投票:0回答:2

我需要解析一个html标签。举个例子:

<a href=“http://www.google.it”>MY LINK</a>

那么,我如何检测此链接,并使其在标签中可点击?只有链接可点击。

谢谢

html objective-c
2个回答
0
投票

你可以使用TextView。使用textView,您将设置attributedText具有属性,即您想要的链接

    NSString *url = @"https://www.stackoverflow.com";
    NSString *stringLink = @"My link";

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringLink attributes:nil];
    [attrString addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, [attrString.string length])];

    self.textView.attributedText = attrString;

编辑:

确保正确格式化您的String以确保在“href = \”“之后捕获您的URL并且在”>“字符之后捕获stringLink。


0
投票

在ViewController.h中尝试这种方式

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

和ViewController.m

#import "ViewController.h"

@interface ViewController ()<UITextViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnTextView)];
    [self.textView addGestureRecognizer:tapGesture];
    [tapGesture setNumberOfTapsRequired:1];
    [self.view addSubview:self.textView];

    self.textView.delegate=self;
    NSString * testStr=@"The world’s most popular camera is more advanced than ever. The 12-megapixel iSight camera captures sharp, detailed photos. It takes brilliant 4K video, up to four times the resolution of 1080p HD video. https://www.google.com/gmail/about/# iPhone 6s also takes selfies worthy of a self-portrait with the new 5-megapixel FaceTime HD camera. And it introduces Live Photos, a new way to relive your favourite memories. It captures the moments just before and after your picture and sets it in motion with just the press of a finger.";
    self.textView.text = testStr;
    self.textView.dataDetectorTypes = UIDataDetectorTypeLink;

}
- (void)openScheme:(NSString *)scheme {
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:scheme];

    if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
        [application openURL:URL options:@{}
           completionHandler:^(BOOL success) {
               NSLog(@"Open %@: %d",scheme,success);
           }];
    } else {
        BOOL success = [application openURL:URL];
        NSLog(@"Open %@: %d",scheme,success);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tapOnTextView
{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.textView.text];
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    [detector enumerateMatchesInString:self.textView.text options:kNilOptions range:NSMakeRange(0, [self.textView.text length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        NSLog(@"Values: %@", result);
        if (result.resultType == NSTextCheckingTypeLink)
        {
            NSLog(@"matched: %@",result.URL);

            [self openScheme:[result.URL absoluteString]];

        }
    }];
    self.textView.attributedText=attributedString;
}
© www.soinside.com 2019 - 2024. All rights reserved.