美文网首页
iOS-富文本实现 和wkweb网页实现

iOS-富文本实现 和wkweb网页实现

作者: LeiLeiString | 来源:发表于2017-12-25 17:21 被阅读0次
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"注册即同意《金购会员服务协议》《金购隐私政策》"];     //可变属性的字符串
    
    [attributedString addAttribute:NSLinkAttributeName
     
                             value:@"huiyuanxieyi://"
     
                             range:[[attributedString string] rangeOfString:@"《金购会员服务协议》"]];     //给可变字符串配置富文本属性
    
    [attributedString addAttribute:NSLinkAttributeName
     
                             value:@"yinsixieyi://"
     
                             range:[[attributedString string] rangeOfString:@"《金购隐私政策》"]];     //同上
    
    //textView的代理方法
    
    -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
        
        if ([[URL scheme] isEqualToString:@"huiyuanxieyi"]){
            
            WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];     //WKWebViewConfiguration是一个具有集合属性的对象,用来初始化web view@帮忙包含用于配置@link WKWebView@/链接的属性
            
            WKUserContentController *userCC = config.userContentController;     //WKUserContentController为对象JavaScript提供一种消息方式到web view。与web view相关联的用户控制器由它指定web配置视图
            
            /*
             
             *此处添加了一个跳转到web页面实现事件
             
             */
            
            //JavaScript调用oc  添加处理脚本
            
            [userCC addScriptMessageHandler:self name:@"ClickFinish"];
            
            WKWebView *webView =[ [WKWebView alloc] initWithFrame:self.view.frame configuration:config];
            
            webView.scrollView.scrollEnabled = YES;
            
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jingomall.cn/xxxxxxxxxxxx.html"]];
            
            [self.view addSubview:webView];
            
            [self.webView loadRequest:request];
            
            self.webView.navigationDelegate= self;
            
            return NO;
            
        }
        
        return YES;
        
    }
    
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
        
        if ([message.name isEqualToString:@"ClickFinish"])
            
        {
            
            [self.webView removeFromSuperview];
            
        }
        
    }
    
    //web返回控制器
    
    - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
        
        //    [self.view addSubview:webView];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        //    self.VIPButton.userInteractionEnabled = YES;
        
        //    self.privacyButton.userInteractionEnabled =YES;
        
    }
    

    相关文章

      网友评论

          本文标题:iOS-富文本实现 和wkweb网页实现

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