Add a regular action to an NSAttributedString?

醉酒当歌 提交于 2019-12-01 06:02:30

问题


I simply want to add an objetive-c action to be called upon a tap on a certain range in my UILabel using NSAttributedString.

But I don't know how to do it. I don't want to open a URL, I just wanna to call a method.

Thanks


回答1:


You can use NSLinkAttributeName itself to achieve this. Use an attributed string as shown below and set it as the text of a UITextView.

 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:termsString];
        [attributedString addAttribute:NSLinkAttributeName value:url range:range];
[myTextView setAttributedText:attributedString];

And then override the UITextView delegate method:

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{

// Call your method here.
return YES;
}

Dont forget to set the delegate of your textView!




回答2:


Also, you can just make a simple transparent UITouch like control on top it. It means that you need to fake it.



来源:https://stackoverflow.com/questions/27908596/add-a-regular-action-to-an-nsattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!