WebView动态插入/修改内容 [英] WebView Insert / Modify Content dynamically

查看:113
本文介绍了WebView动态插入/修改内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用WebView显示内容,
现在可以动态修改内容,需求是这样的,

In my application, i am using WebView to display the content, Now is it possible to modify the content dynamically, the requirement is something like this,

i将从网络获取信息,根据他们,我需要设置style / font /属性,或者当连接的设备没有响应时,我可能需要添加新的文本,

i will get info from network and depending upon them i need to set the style/font/attribute or probably i need to append the new text when the connected device is not responding,

到目前为止我使用下面的代码,

So far i am using following code,

-(void)modifyString:(NSString *)string{
   [sourceString stringByAppendingString:errorString :string] 
}

   -(void)reloadPage{

     [[pWebView mainFrame] loadHTMLString:htmlString baseURL:nil];
    }



我不认为它的正确方式来实现它,我试图使用

I don’t think its correct way to implement it, i am trying to make use of

[pWebView replaceSelectionWithMarkupString:@"<html><body><p>Hi there </p></br></body></html>"];

没有显示,因为,我没有选择和选择我的问题是
如何设置选择?

but nothing is displaying because, i have not selected and to select my question is How can i set the selection ?

亲爱的

罗汉

推荐答案

不介意
以这种方式解决

在AwakeFromNib方法中,添加以下代码:

Never Mind Solved it by this way
In AwakeFromNib Method, added following code,

-(void)awakeFromNib{

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"about:blank"]];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [[pWebView mainFrame ]loadRequest:requestObj];
    [pWebView setEditable:YES];
    [pWebView setNeedsDisplay:YES];

}

并添加此函数以追加body元素, p>

and added this function to append the body element,

-(void)appendTagToBody:(NSString *)tagName InnerHTML:(NSString *)innerHTML
{
    // Gets a list of all <body></body> nodes.
    DOMNodeList *bodyNodeList = [[[pWebView mainFrame] DOMDocument] getElementsByTagName:@"body"];

    // There should be just one in valid HTML, so get the first DOMElement.
    DOMHTMLElement *bodyNode = (DOMHTMLElement *) [bodyNodeList item:0];

    // Create a new element, with a tag name.
    DOMHTMLElement *newNode = (DOMHTMLElement *) [[[pWebView mainFrame] DOMDocument] createElement:tagName];

    // Add the innerHTML for the new element.
    [newNode setInnerHTML:innerHTML];

    // Add the new element to the bodyNode as the last child.
    [bodyNode appendChild:newNode];
}

,只要想更改内容,

-(void)appendString:(NSString *)pString{
    [self appendTagToBody:@"div" InnerHTML:@"<div><p> Hi there </p></div>"];
    [self setNeedsDisplay:YES];
}

这篇关于WebView动态插入/修改内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆