웹보기에 HTML 콘텐츠를 추가하려고했지만 아무런 결과도 얻지 못했습니다. Apple 설명서는 매우 희박하며 few 예제 중 online을 찾았습니다. 그 중 어떤 것도 작동하지 않는 것 같습니다. 제가 말할 수있는 한, 이것이 어떻게 작동해야하는지는 모르지만 그렇게하지는 않습니다.코코아 Webview에 HTML 요소를 추가하는 방법
시작하려면 웹보기에로드하는 매우 간단한 HTML 문서가 있습니다. #container div에 일부 HTML 콘텐츠를 추가하려고합니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>HTML TEST</title>
<style type="text/css">
.test {
display: block;
border: #000 1px solid;
width: 100px;
height: 100px;
background-color: #0F0;
}
</style>
</head>
<body>
<p>hello world</p>
<div id="container"></div>
</body>
</html>
WebView는 사용자 정의보기 안에 있습니다. 헤더는 매우 똑바르다.
// NodeTest.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface NodeTest : NSView
@property (nonatomic, retain) IBOutlet WebView *webview;
@end
그래서,로 awakeFromNib에서, 나는, 컨테이너 사업부를 얻을 클래스 이름 "테스트"와, 그것을 내로 새로운 HTML 요소를 삽입하는 시도 기본 HTML 문서를로드. HTML 파일에 따르면 검은 색 테두리가 있어야하며 녹색으로 채우기가 100x100px 여야합니다.
#import "NodeTest.h"
@implementation NodeTest
@synthesize webview;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void) awakeFromNib {
// Load the HTML document into the webview
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"TestDocument" ofType:@"html"]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[webview mainFrame] loadRequest:req];
[webview setEditable:YES];
DOMDocument *domDoc = [[webview mainFrame] DOMDocument];
// Get the container div. I've tried this with and without '#'
DOMHTMLElement *container = (DOMHTMLElement *)[domDoc getElementById:@"container"];
// Create a new HTML element
DOMHTMLElement *myDiv = (DOMHTMLElement *)[domDoc createElement:@"div"];
[myDiv setInnerHTML:@"<p>Boo!</p>"];
[myDiv setClassName:@"test"];
// Add the element to the container div
[container appendChild:myDiv];
[webview setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
@end
HTML 내용을 WebView에로드하는 것이 좋습니다. 모든 것이 있습니다. 하지만 나는 내 삶에 새로운 HTML 요소를 추가하는 방법을 생각할 수 없습니다. 누군가 포인터가 있다면, 나는 그들을 듣고 싶습니다. 이것은 나를 미치게합니다.
우수 감사합니다. 쉬운 경로를 선택하고 HTML 요소를 만드는 버튼을 추가했습니다. HTML 요소를 생각하면 처음 시도한 것 중 하나 였을 것입니다. – gargantuan