美文网首页ios开发的那些坑
iOS-UITextView富文本点击事件

iOS-UITextView富文本点击事件

作者: fly大梦想家 | 来源:发表于2018-11-20 13:07 被阅读3次
    uitextview文本点击.gif
          NSString *policyPromptStr = @"I agree to the Terms of Use and Privacy Policy.";
        NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor blackColor]};
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:policyPromptStr attributes:attributes];
        [attStr addAttributes:@{NSForegroundColorAttributeName :[UIColor redColor],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@"Terms of Use"} range:NSMakeRange(15, 12)];
        [attStr addAttributes:@{NSForegroundColorAttributeName :[UIColor redColor],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@"Privacy Policy"} range:NSMakeRange(32, 14)];
        
        UITextView *policyTextView = [[UITextView alloc] initWithFrame:CGRectMake(20,100,260,30)];
        policyTextView.editable = NO;
        policyTextView.scrollEnabled = NO;
        policyTextView.attributedText = attStr;
        policyTextView.delegate = self;
        policyTextView.textContainerInset = UIEdgeInsetsMake(5, 0,0,0);
        [self.view addSubview:policyTextView];
    

    代理里执行事件

    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
        if (characterRange.location == 15 && characterRange.length == 12) {
           NSLog(@"Terms of use was clicked");
        }else if (characterRange.location == 32 && characterRange.length == 14){
            NSLog(@"Privacy Policy was clicked");
        }
        return NO;
    }
    
    返回yes的话并长按会crash

    相关文章

      网友评论

        本文标题:iOS-UITextView富文本点击事件

        本文链接:https://www.haomeiwen.com/subject/ijutqqtx.html