iOS 通过textview实现部分字符串赋予点击事件

最常见的app登录页会有一行“请阅读并同意《用户协议》和《隐私政策》”,并且《用户协议》和《隐私政策》字体颜色不一样,还要绑定点击事件,跳转查看协议。

那么这种情况通过多控件实现比较麻烦,直接通过textview控件即可实现。

一、创建textview,并禁掉编辑、滑动等功能

    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, 200, 100)];

    [self.view addSubview:self.textView];

    self.textView.backgroundColor = [UIColor lightGrayColor];

    self.textView.editable=NO;

    self.textView.scrollEnabled = NO;

    self.textView.selectable=NO;

    self.textView.delegate=self;

二、创建字符串,并将要添加点击事件的字符串赋予关键字。为textview添加手势,通过关键字区分点击事件

    NSString*fristStr =@"我已阅读并同意";

    NSString*secondStr =@"《用户协议》";

    NSString*thirdStr =@"、";

    NSString*fourthStr =@"《隐私政策》";


    NSString*allStr = [NSStringstringWithFormat:@"%@%@%@%@",fristStr,secondStr,thirdStr,fourthStr];


    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:allStr];

    //给需要点击的部分添加关键字

    [attributedStringaddAttribute:NSLinkAttributeNamevalue:@"firstmanager"range:[[attributedStringstring]rangeOfString:secondStr]];

    [attributedStringaddAttribute:NSLinkAttributeNamevalue:@"secondmanager"range:[[attributedStringstring]rangeOfString:fourthStr]];

    //赋值

    self.textView.attributedText= attributedString;


    //设置点击部分的文字颜色

    self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor orangeColor]};

    UITapGestureRecognizer*textTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(addGestureRecognizer:)];

    [self.textView addGestureRecognizer:textTap];

三、手势函数实现

#pragma mark -- textVeiw 手势

-(void)addGestureRecognizer:(UITapGestureRecognizer*)gestureRecognizer{


    if([gestureRecognizerisKindOfClass:[UITapGestureRecognizerclass]]) {


        CGPointtopLocation = [gestureRecognizerlocationInView:self.textView];

        UITextPosition*textPosition = [self.textViewclosestPositionToPoint:topLocation];

        NSDictionary *attributes = [self.textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionBackward];

        NSURL*url = attributes[NSLinkAttributeName];


        if(url) {

            NSRangerange = [self.textView.textrangeOfString:@"《用户协议》"];


            if([urlisKindOfClass:[NSStringclass]] && [(NSString*)urlisEqualToString:@"firstmanager"]){


                range = [self.textView.textrangeOfString:@"《用户协议》"];


            }elseif([urlisKindOfClass:[NSStringclass]] && [(NSString*)urlisEqualToString:@"secondmanager"]){


                range = [self.textView.textrangeOfString:@"《隐私政策》"];

            }


            [self textViews:self.textView shouldInteractWithURL:url inRange:range];

        }

    }

}

- (void)textViews:(UITextView*)textViewshouldInteractWithURL:(NSURL*)URLinRange:(NSRange)characterRange{

    if([(NSString*)URLisEqualToString:@"firstmanager"]) {

        NSLog(@"点击的是《用户协议》");


    }elseif([(NSString*)URLisEqualToString:@"secondmanager"]) {

        NSLog(@"点击的是《隐私政策》");

    }

}

你可能感兴趣的:(iOS 通过textview实现部分字符串赋予点击事件)