gpt4 book ai didi

cocoa - WebView动态插入/修改内容

转载 作者:行者123 更新时间:2023-12-03 16:24:05 27 4
gpt4 key购买 nike

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

我将从网络获取信息,根据它们,我需要设置样式/字体/属性,或者当连接的设备没有响应时,我可能需要附加新文本,

到目前为止我正在使用以下代码,

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

-(void)reloadPage{

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

我认为这不是正确的实现方式,我正在尝试使用

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

但没有显示任何内容,因为我还没有选择,要选择我的问题是我如何设置选择?

亲切的问候

罗汉

最佳答案

没关系就这样解决了
在AwakeFromNib方法中,添加以下代码,

-(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 元素,

-(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];
}

关于cocoa - WebView动态插入/修改内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5311298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com