我需要解析一个html标签。例如:
<a href=“http://www.google.it”>MY LINK</a>
那么,如何检测此链接,并使其在标签中可点击?只有链接可点击。
由于
答案 0 :(得分:0)
您可以使用TextView。使用textView,您将设置attributionText具有您想要的链接属性
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;
编辑:
请确保正确格式化您的字符串,以确保在“href = \”“之后捕获您的网址,并在”&gt;“字符之后捕获stringLink。
答案 1 :(得分: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;
}