点击部分UILabel的手势
我可以使用以下代码成功地将轻拍手势添加到UITextView的一部分:
UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView for (int i=0;i<words*2-1;i++){// *2 since UITextGranularityWord considers a whitespace to be a word UITextPosition *pos2 = [textView.tokenizer positionFromPosition:pos toBoundary:UITextGranularityWord inDirection:UITextLayoutDirectionLeft]; UITextRange *range = [textView textRangeFromPosition:pos toPosition:pos2]; CGRect resultFrame = [textView firstRectForRange:(UITextRange *)range ]; UIView* tapViewOnText = [[UIView alloc] initWithFrame:resultFrame]; [tapViewOnText addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetRoutine)]]; tapViewOnText.tag = 125; [textView addSubview:tapViewOnText]; pos=pos2; }
我希望在UILabel
模仿相同的行为。 问题是,在UITextInput.h
声明了UITextInputTokenizer
(用于标记各个单词),并且只有UITextView
& UITextField
符合UITextInput.h
; UILabel
没有。 有没有解决方法?
你可以尝试https://github.com/mattt/TTTAttributedLabel并添加一个链接到标签。 当链接被按下时,你会得到一个动作,所以标签点击的部分工作只是你必须自定义标签的链接部分。 我在过去尝试过,它的工作完美无瑕,但是我的客户对使用第三方组件并不感兴趣,所以使用UIWebView和HTML重复了这个function。
尝试这个。 让你的标签成为label
:
//add gesture recognizer to label UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init]; [label addGestureRecognizer:singleTap]; //setting a text initially to the label [label setText:@"hello world i love iphone"]; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.view]; CGRect rect = label.frame; CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/2, rect.size.height); if (CGRectContainsPoint(newRect, touchPoint)) { NSLog(@"Hello world"); } }
点击标签的前半部分将工作(它给出日志输出)。 不是另一半。
一种select是使用不可编辑的UITextView而不是UILabel。 当然这可能会也可能不是一个合适的解决scheme,具体取决于您的具体需求。
这里是一个轻量级的图书馆,专门用于UILabel FRHyperLabel中的链接。
要达到这样的效果:
Lorem ipsum dolor sit amet,consectetur adipiscing elit。 Pellentesque quis blandit爱神,坐amet车辆justo。 Nam在urna neque。 Maecenas ac sem eu sem porta dictum nec prec de tellus。
使用代码:
//Step 1: Define a normal attributed string for non-link texts NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis blandit eros, sit amet vehicula justo. Nam at urna neque. Maecenas ac sem eu sem porta dictum nec vel tellus."; NSDictionary *attributes = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]}; label.attributedText = [[NSAttributedString alloc]initWithString:string attributes:attributes]; //Step 2: Define a selection handler block void(^handler)(FRHyperLabel *label, NSString *substring) = ^(FRHyperLabel *label, NSString *substring){ NSLog(@"Selected: %@", substring); }; //Step 3: Add link substrings [label setLinksForSubstrings:@[@"Lorem", @"Pellentesque", @"blandit", @"Maecenas"] withLinkHandler:handler];
这是如何将UITapGestureRecognizer
添加到您的控件的基本代码;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [MyLabelName addGestureRecognizer:singleTap]; [self.view addSubView:MyLabelName]
这是您点击MyLabelName
时调用的方法;
-(void)handleSingleTap:(UILabel *)myLabel { // do your stuff; }