我在这个问题中学到了 NAlexN 的答案(在 UILabel 的 NSAttributedString 中创建可点击的“链接”?),并自己编写了一个演示。然而,我很不幸地完成了这个演示。这是我的代码:(大家可以直接将以下代码复制到新项目中进行测试)
#import "ViewController.h"
@interface ViewController ()
{
UILabel* label;
NSTextContainer *textContainer;
NSLayoutManager *layoutManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[super viewDidLoad];
label=[[UILabel alloc]initWithFrame:CGRectMake(15, 30, 350, 300)];
label.backgroundColor=[UIColor greenColor];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapOnLabel:)];
[tap setNumberOfTapsRequired:1];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:@"String with a link" attributes:nil];
NSRange linkRange = NSMakeRange(14, 4); // for the word "link" in the string above
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName …Run Code Online (Sandbox Code Playgroud)