我只是想添加一个 objetive-c 操作,以便使用 NSAttributedString 在我的 UILabel 中的某个范围内点击时调用。
但是我不知道该怎么做。我不想打开一个 URL,我只想调用一个方法。
谢谢
最佳答案
您可以使用 NSLinkAttributeName 本身来实现这一点。使用如下所示的属性字符串并将其设置为 UITextView 的文本。
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:termsString];
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
[myTextView setAttributedText:attributedString];
然后覆盖 UITextView 委托(delegate)方法:
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
// Call your method here.
return YES;
}
不要忘记设置 textView 的委托(delegate)!
关于ios - 向 NSAttributedString 添加常规操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27908596/