如何在UITextView中检测自定义数据模式,例如我在UITextView中有这个文本
textView.text = @"Finally you guys won !! @Chock @Sanda #winners #amazing";
我想在用户点击以@或#开头的字词时收到通知。谢谢
答案 0 :(得分:1)
我希望这个库可以解决您的问题:
参考: https://github.com/rootd/AMAttributedHighlightLabel
AMAttributedHighlightLabel *tweetLabel = [[AMAttributedHighlightLabel alloc] initWithFrame:CGRectMake(..,..,..,..)];
tweetLabel.delegate = self;
tweetLabel.userInteractionEnabled = YES;
tweetLabel.numberOfLines = 0;
tweetLabel.lineBreakMode = NSLineBreakByCharWrapping;
[tweetLabel setString:@"This #is a @test for my #@new http://AMAttributedHighlightLabel.class"];
tweetLabel.textColor = [UIColor lightGrayColor];
tweetLabel.mentionTextColor = [UIColor darkGrayColor];
tweetLabel.hashtagTextColor = [UIColor darkGrayColor];
tweetLabel.linkTextColor = [UIColor colorWithRed:129.0/255.0 green:171.0/255.0 blue:193.0/255.0 alpha:1.0];
tweetLabel.selectedMentionTextColor = [UIColor blackColor];
tweetLabel.selectedHashtagTextColor = [UIColor blackColor];
tweetLabel.selectedLinkTextColor = UIColorFromRGB(0x4099FF);
NSError *error;
regex = [NSRegularExpression regularExpressionWithPattern:@"((@|#)([A-Z0-9a-z(é|ë|ê|è|à|â|ä|á|ù|ü|û|ú|ì|ï|î|í)_]+))|(http(s)?://([A-Z0-9a-z._-]*(/)?)*)" options:NSRegularExpressionCaseInsensitive error:&error];
tweetLabel.regex = regex;
[self.view addSubview:tweetLabel];