[英]Tap Gesture on part of 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
模仿相同的行为。 问题是, UITextInputTokenizer
(使用来标记单个单词)在声明UITextInput.h
,只有UITextView
& UITextField
符合UITextInput.h
; UILabel
没有。 有没有解决方法?
尝试这个。 让您的标签成为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");
}
}
单击标签的前半部分将起作用(它提供日志输出)。 没有另一半。
这是一个轻量级的库,专门用于UILabel FRHyperLabel中的链接。
为了达到这样的效果:
Lorem ipsum dolor坐下,一直保持着安静的状态。 Pellentesque quis blandit eros,坐在amet vehicula justo。 Nam at urna neque。 Maecenas ac sem eu sem porta dictum nev veltellus。
使用代码:
//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];
一种选择是使用不可编辑的UITextView代替UILabel。 当然,根据您的确切需求,这可能是不合适的解决方案,也可能不是。
您可以尝试https://github.com/mattt/TTTAttributedLabel并添加指向标签的链接。 当按下链接时,您将执行操作,因此单击标签的一部分仅对自定义标签的链接部分有效。 我过去曾尝试过此方法,但它工作得很好,但是我的客户对使用第三方组件不感兴趣,因此使用UIWebView和HTML复制了此功能。
这是有关如何将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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.