2013-07-24 27 views
5

tôi có thể thành công thêm cử chỉ tap vào một phần của UITextView với đoạn mã sau:Tap Gesture trên một phần của 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; 
} 

Tôi muốn bắt chước các hành vi tương tự trong một UILabel. Vấn đề là, UITextInputTokenizer (được sử dụng để mã hóa từng từ riêng lẻ) được khai báo trong UITextInput.h và chỉ UITextView & UITextField phù hợp với UITextInput.h; UILabel thì không. Có cách nào khác không?

+0

hi bạn bè, có bạn kiểm tra các hành vi tương tác người dùng của UILabel, bởi vì userinteraction mặc định là NO của UILabel, bạn sẽ phải thiết lập nó YES.Let tôi biết là nó làm việc hay không.!!! – NiravPatel

+0

Hành động trên toàn bộ UILabel không phải là vấn đề, đó là 'một phần' của UILabel. – n00bProgrammer

+0

Vui lòng kiểm tra http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger

Trả lời

0

Bạn có thể thử https://github.com/mattt/TTTAttributedLabel và thêm liên kết vào nhãn. Khi liên kết được nhấn, bạn nhận được một hành động, do đó, một phần của nhấp chuột nhãn chỉ hoạt động mà bạn phải tùy chỉnh phần liên kết của nhãn. Tôi đã thử điều này trong quá khứ và nó hoạt động hoàn hảo nhưng khách hàng của tôi không quan tâm đến việc sử dụng một thành phần của bên thứ ba để sao chép chức năng này bằng UIWebView và HTML.

+0

Cuối cùng được giải quyết trên TTTAttributedLabel. Hóa ra nó nặng như một UITextView. :( – n00bProgrammer

+1

TTTAttributedLabel có vấn đề về độ cao. Đặc biệt khi nói đến biểu tượng cảm xúc. – Mahouk

-2

Đây là mã cơ bản để biết cách thêm UITapGestureRecognizer vào kiểm soát của bạn;

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];   
[MyLabelName addGestureRecognizer:singleTap];   
[self.view addSubView:MyLabelName] 

Đây là phương thức gọi khi bạn chạm vào MyLabelName;

-(void)handleSingleTap:(UILabel *)myLabel 
{ 
    // do your stuff; 
} 
+0

Tôi đánh giá cao câu trả lời của bạn, nhưng tôi không cần một cử chỉ trên toàn bộ nhãn, chỉ là một phần của nó. – n00bProgrammer

2

Một tùy chọn sẽ là sử dụng UITextView không thể chỉnh sửa thay vì UILabel. Tất nhiên điều này có thể hoặc có thể không phải là một giải pháp phù hợp tùy thuộc vào nhu cầu chính xác của bạn.

+0

UITextViews không hoạt động. Tôi chỉ có thể sử dụng UILabels. – n00bProgrammer

+2

@ n00bProgrammer, bạn có thể làm cho UITextView hoạt động như UILabel. – Vignesh

4

Hãy thử điều này. Hãy nhãn của bạn được 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"); 
} 
} 

Nhấp vào nửa đầu của nhãn sẽ làm việc (Nó cung cấp dữ liệu ghi nhận). Không phải nửa kia.

+0

@ n00bProgrammer - Bạn có thể tạo riêng 'CGRect newRect' dựa trên nhu cầu của bạn. – iphondroid

2

Đây là thư viện có trọng số nhẹ dành riêng cho các liên kết trong UILabel FRHyperLabel.

Để đạt được một hiệu ứng như thế này:

Lorem ipsum dolor sit Amet, consectetur adipiscing elit. Pellentesque quis blandit eros, hãy ngồi ngay lập tức. Nam tại urna neque. Maecenas ac sem eu sem porta dictum nec vel tellus.

đang sử dụng:

//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]; 
Các vấn đề liên quan