Perform action by clicking on some word in Uitextview or UILabel

后端 未结 3 1120
清歌不尽
清歌不尽 2020-12-06 03:58

How can I do to perform some specific action (like showing a modal or pushing a controller) when user click on some formated/specific word in Uitextview (or UIlabel) ? I\'ve

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 04:23

    It's hackish, but you can try using TTTAttributedLabel and attach a custom URL to the word/phrase within the label:

        TTTAttributedLabel *label;
        //after setting the label text:
        [label addLinkToURL:[NSURL URLWithString:@"http://www.stackoverflow.com"] withRange:[label.text rangeOfString:@"CLICKABLE TEXT HERE"]];
    

    Then in the delegate method, you call your selected action:

    #pragma mark - TTTAttributedLabelDelegate
    
    - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
        // for handling the URL but we just call our action
        [self userHasClickedTextInLabel];
    }
    

提交回复
热议问题