2013-07-24 60 views
5

的一部分,我可以成功添加自来水手势的UITextView的一部分与下面的代码:双击手势上的UILabel

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没有。 有没有解决这个问题的方法?

+0

喜的朋友,你检查的UILabel的用户交互行为,因为默认情况下用户交互是NO的UILabel的,你必须把它YES.Let我知道的是它的工作或不。!!! – NiravPatel

+0

对整个UILabel的操作不是问题,它是UILabel的一部分。 – n00bProgrammer

+0

请检查http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger

回答

0

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

+0

最后落户于TTTAttributedLabel。事实证明,它和UITextView一样重。 :( – n00bProgrammer

+1

TTTAttributedLabel有高度问题,特别是当涉及到表情符号。 – Mahouk

-2

这是如何将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; 
} 
+0

我很欣赏你的答案,但我不需要整个手势标签,只是其中的一部分。 – n00bProgrammer

2

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

+0

UITextViews没有问题。我只能使用UILabels。 – n00bProgrammer

+2

@ n00bProgrammer,你可以使UITextView的行为像UILabel。 – Vignesh

4

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

点击标签上半年将工作(这使日志输出)。不是另一半。

+0

@ n00bProgrammer - 您可以根据自己的需求创建自己的CGRect newRect。 – iphondroid

2

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

为了达到这样的效果:

Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。 Pellentesque quis blandit爱神,坐amet vehicula justo。 Nam在urna neque。 Maecenas ac sem eu sem porta dictum nec prec 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];