问题描述
我可以使用以下代码成功地将轻击手势添加到UITextView的一部分:
I could successfully add tap gestures to a part of UITextView with the following code:
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
中声明,并且只有 UITextView
& UITextField
符合 UITextInput.h
; UILabel
没有。
这是否有解决办法?
I wish to imitate the same behaviour in a UILabel
. The issue is, UITextInputTokenizer
(used to tokenize the individual words) is declared in UITextInput.h
, and only UITextView
& UITextField
conform to UITextInput.h
; UILabel
does not.Is there a workaround for this ??
推荐答案
您可以尝试并添加标签链接。当按下链接时,你会得到一个动作,因此标签点击的一部分只能用于自定义标签的链接部分。我在过去尝试了这个并且它完美无缺,但是我的客户对使用第三方组件不感兴趣,所以使用UIWebView和HTML复制了这个功能。
You could try https://github.com/mattt/TTTAttributedLabel and add a link to the label. When the link is pressed you get a action, so part of the label click works only thing you have to would be customizing the link part of the label. I tried this in the past and it worked flawlessly but my client was not interested in using a third party component so duplicated this functionality using UIWebView and HTML.
这篇关于点击UILabel部分的手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!