6

Tôi có một NSAttributedString như vậy:Thêm một cử chỉ tap vào một phần của một UILabel

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; 
NSInteger length = str.length; 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)]; 

Các NSMutableAttributedString được thiết lập để một UILabel như vậy:

label.attributedText = str; 

Làm thế nào để làm cho một vòi nước cử chỉ (hoặc một cái gì đó có thể nhấp) để viewController khác cho @clickhere của từ trong chuỗi ở trên?

Cảm ơn!

Trả lời

5

Tôi nghĩ rằng, cách tốt nhất là thêm các UIGestureRecognizer để UILabel của bạn và xác thực khung mà bạn muốn.

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
[_yourLabel addGestureRecognizer:singleTap]; 

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer 
{ 
    CGPoint touchPoint = [tapRecognizer locationInView: _yourLabel]; 

    //Modify the validFrame that you would like to enable the touch 
    //or get the frame from _yourLabel using the NSMutableAttributedString, if possible 
    CGRect validFrame = CGRectMake(0, 0, 300, 44); 

    if(YES == CGRectContainsPoint(validFrame, touchPoint) 
    { 
     //Handle here. 
    } 
} 
1

Đơn giản chỉ cần trước hết phải thêm một cử chỉ để nhãn của bạn

[label setUserInteractionEnabled:YES]; 
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
[label addGestureRecognizer:gesture]; 

kiểm soát khu vực cử chỉ của bạn trong các phương pháp dưới đây

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
{ 
    static CGRect touchableRect = CGRectMake(100.0f, 0.0f, 100.0f, 50.0f); // Give your rect as you need. 
    CGPoint touchPoint = [gestureRecognizer locationInView:self.view]; 
    if (CGRectContainsPoint(touchableRect, touchPoint)) 
    { 
     //User has tap on where you want. Do your other stuff here 
    } 
} 
0
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)]; 
Tap.numberOfTapsRequired = 1; 
// label Name Is your Label Name 
[labelName addGestureRecognizer:Tap]; 
-(void)tapDetected 
    { 
    //your code 
    } 
0

Tôi chỉ muốn thêm vào câu trả lời của Ramshad, về cách đối phó với khung hợp lệ.

Đối với điều này bạn có thể muốn xem xét sử dụng UITextView thay vì UILabel, mà không cung cấp cho bạn truy cập vào nó như thế nào quản lý bố trí văn bản. Bằng cách tắt biên tập, lựa chọn và di chuyển, UITextView cư xử gần giống như một UILabel, ngoại trừ một số padding bạn phải gỡ bỏ.

Để thuận tiện, bạn có thể muốn thêm một danh mục nhỏ vào UITextView, trong đó bạn viết một phương pháp để kiểm tra xem một điểm có chạm vào bất kỳ ký tự nào trong phạm vi không.

- (BOOL)point:(CGPoint)point touchesSomeCharacterInRange:(NSRange)range 
{ 
    NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:range actualCharacterRange:NULL]; 

    BOOL touches = NO; 
    for (NSUInteger index = glyphRange.location; index < glyphRange.location + glyphRange.length; index++) { 
     CGRect rectForGlyphInContainer = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(index, 1) inTextContainer:self.textContainer]; 
     CGRect rectForGlyphInTextView = CGRectOffset(rectForGlyphInContainer, self.textContainerInset.left, self.textContainerInset.top); 

     if (CGRectContainsPoint(rectForGlyphInTextView, point)) { 
      touches = YES; 
      break; 
     } 
    } 

    return touches; 
} 

Điều này cũng sẽ làm việc cho một đoạn văn bản chứa nhiều từ trải rộng trên nhiều dòng do bọc từ. Nó cũng sẽ làm việc trên các văn bản được bản địa hóa khi chúng ta xử lý các ký tự được in.

+0

Hi Li, cậu kiểm tra mã này? – Zeb

Các vấn đề liên quan