UITextView -实现一句话里某段文字可以点击跳转转链接

给UItextView注入富文本

//    NSObliquenessAttributeName  字体倾斜程度
//    NSBackgroundColorAttributeName   文字的背景色,不是文字的颜色
//    NSForegroundColorAttributeName  文字颜色
//    NSKernAttributeName  文字间距
//    NSStrikethroughStyleAttributeName  删除线
//    NSUnderlineStyleAttributeName  下划线
//    NSBaselineOffsetAttributeName  基线偏移,负值:向下偏移
    NSMutableAttributedString * atrStr = [[NSMutableAttributedString alloc] initWithString:@"商城暂不支持手机注册,请前往PC端或微商城注册"];
    [atrStr addAttribute:NSForegroundColorAttributeName
                   value:[UIColor redColor]
                   range:NSMakeRange(0, atrStr.length - 5)];
    [atrStr addAttribute:NSBaselineOffsetAttributeName
                   value:[NSNumber numberWithFloat:-3]
                   range:NSMakeRange(0, atrStr.length)];
    [atrStr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
//                         [NSNumber numberWithInt:1],NSUnderlineStyleAttributeName,   // 下划线
//                         [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName, // 基线偏移,负值:向下偏移
                           [YSCUiUtils fontTwo], NSFontAttributeName,
                           [NSURL URLWithString:[YSCBaseMURL stringByAppendingString:@"/register.html"]], NSLinkAttributeName,
                           nil] range:NSMakeRange(atrStr.length - 5, 3)];
    
    UITextView *textView = [[UITextView alloc]init];
    textView.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor redColor]};
    textView.attributedText = atrStr;
    textView.delegate = self;
    textView.editable = NO;
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [YSCUiUtils colorOne];
    textView.font = [YSCUiUtils fontTwo];
    [self.view addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(Interval);
        make.top.equalTo(self.view.mas_top).offset(Interval);
        make.right.equalTo(self.view.mas_right).offset(-Interval);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    return YES;
}

你可能感兴趣的:(UITextView -实现一句话里某段文字可以点击跳转转链接)