5

我可以使用以下代码成功地将点击手势添加到 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;
}

我希望在 a 中模仿相同的行为UILabel。问题是,UITextInputTokenizer(用于标记单个单词)在 中声明UITextInput.h,并且只有UITextView&UITextField符合UITextInput.h; UILabel才不是。有解决方法吗?

4

5 回答 5

3

尝试这个。让你的标签是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");
}
}

单击标签的前半部分将起作用(它提供日志输出)。不是另一半。

于 2013-07-24T10:04:47.257 回答
3

这是一个轻量级库,专门用于 UILabel FRHyperLabel中的链接。

要达到这样的效果:

Lorem ipsum dolor sit amet, consectetur adipiscing elit。Pellentesque quis blandit eros,坐在 amet vehicula justo。Nam at urna neque。Maecenas ac sem eu sem porta dictum nec vel 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];
于 2015-09-23T17:44:32.957 回答
2

One option would be to use a non-editable UITextView instead of a UILabel. Of course this may or may not be a suitable solution depending on your exact needs.

于 2013-07-24T07:00:44.787 回答
0

您可以尝试https://github.com/mattt/TTTAttributedLabel并添加标签链接。当链接被按下时,你会得到一个动作,所以标签点击的一部分只起作用,你必须自定义标签的链接部分。我过去试过这个,它工作得很好,但我的客户对使用第三方组件不感兴趣,所以使用 UIWebView 和 HTML 复制了这个功能。

于 2013-07-24T07:46:37.860 回答
-3

这是如何添加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;
}
于 2013-07-24T06:51:48.707 回答