【问题标题】:Tap Gesture on part of UILabel点击 UILabel 部分的手势
【发布时间】:2013-07-24 06:48:32
【问题描述】:

我可以使用以下代码成功地将点击手势添加到 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 中模仿相同的行为。问题是,UITextInputTokenizer(用于标记单个单词)在UITextInput.h 中声明,并且只有UITextViewUITextField符合UITextInput.hUILabel 没有。 有解决办法吗??

【问题讨论】:

  • 你好朋友,你有没有检查过UILabel的用户交互行为,因为默认情况下用户交互是UIlabel的NO,你必须设置它YES。让我知道它是否有效。!!!
  • 对整个 UILabel 的操作不是问题,它是 UILabel 的“一部分”。

标签: iphone ios uilabel uitapgesturerecognizer uitextinput


【解决方案1】:

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

点击标签的前半部分会起作用(它会给出日志输出)。不是另一半。

【讨论】:

  • @n00bProgrammer - 您可以根据需要创建自己的CGRect newRect
【解决方案2】:

这里是一个轻量级的库,专门用于 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];

【讨论】:

    【解决方案3】:

    一种选择是使用不可编辑的 UITextView 而不是 UILabel。当然,这可能是也可能不是合适的解决方案,具体取决于您的具体需求。

    【讨论】:

    • UITextViews 是没有问题的。我只能使用 UILabels。
    • @n00bProgrammer,你可以让 UITextView 表现得像 UILabel。
    【解决方案4】:

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

    【讨论】:

    • 终于选择了 TTTAttributedLabel。事实证明它和 UITextView 一样重。 :(
    • TTTAttributedLabel 存在高度问题。尤其是在表情符号方面。
    【解决方案5】:

    这是如何将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-02-08
    • 2014-06-09
    • 2013-06-23
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多