向NSAttributedString添加常规操作?

Ton*_*riz 3 nsattributedstring uilabel ios

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

但我不知道该怎么做.我不想打开URL,我只是想调用一个方法.

谢谢

小智 11

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

 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:termsString];
        [attributedString addAttribute:NSLinkAttributeName value:url range:range];
[myTextView setAttributedText:attributedString];
Run Code Online (Sandbox Code Playgroud)

然后覆盖UITextView委托方法:

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

// Call your method here.
return YES;
}
Run Code Online (Sandbox Code Playgroud)

别忘了设置textView的委托!