向NSAttributedString添加常规操作? [英] Add a regular action to an NSAttributedString?

查看:87
本文介绍了向NSAttributedString添加常规操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在我的UILabel中使用NSAttributedString添加一个objetive-c动作来调用某个范围。

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.

谢谢

推荐答案

您可以使用NSLinkAttributeName本身来实现此目的。使用如下所示的属性字符串并将其设置为UITextView的文本。

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];

然后覆盖UITextView委托方法:

And then override the UITextView delegate method:

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

// Call your method here.
return YES;
}

别忘了设置textView的委托!

Dont forget to set the delegate of your textView!

这篇关于向NSAttributedString添加常规操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆