2012-10-08 25 views
5

我有一張表格segue,應該顯示硬編碼的html頁面。 現在我有兩個部分的問題第一個問題是如何在UIWebView上顯示html內容IOS

<CENTER><H1>A Simple Sample Web Page</H1><H4>By Frodo</H4> 
<H2>Demonstrating a few HTML features</H2></CENTER> 

顯示像

enter image description here

我可以在它的工作原理這種情況下,改變<CENTER>標籤<left>但是它限制了我的選擇。如何顯示此內容在屏幕中間沒有對齊問題?

問題的第二部分是如何嵌入顏色或內容鏈接。在線""FFFFFF">\n"我得到那個說Expected ':'和在線"<a href="http://somegreatsite.com">\n"一半的線是綠色的是好像這意味着它不會通過的XCode執行

我完整的代碼一樣在屏幕上註釋錯誤:

- (void) createWebViewWithHTML{ 
    //create the string 
    NSMutableString *html = [NSMutableString stringWithString: @"<html><head><title></title></head><body style=\"background:transparent;\">"]; 

    //continue building the string 
    [html appendString:@"<BODY BGCOLOR=" 
    ""FFFFFF">\n" 
    "<CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM"> 
    "</CENTER> \n" 
    "<HR>\n" 
    "<a href="http://somegreatsite.com">\n" 
    "Link Name</a>\n" 

    "is a link to another nifty site\n" 

    "<H1>This is a Header</H1>\n" 

    "<H2>This is a Medium Header</H2>\n" 

    "Send me mail at <a href="mailto:[email protected]">\n" 

    "[email protected]</a>.\n" 

    "<P> This is a new paragraph!\n" 

    "<P> <B>This is a new paragraph!</B>\n" 

    "<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>\n" 

    "<HR>\n"]; 
    [html appendString:@"</body></html>"]; 

    //instantiate the web view 
    webView = [[UIWebView alloc] initWithFrame:self.view.frame]; 


    //make the background transparent 
    [webView setBackgroundColor:[UIColor clearColor]]; 

    //pass the string to the webview 
    [webView loadHTMLString:[html description] baseURL:nil]; 

    //add it to the subview 
    [self.view addSubview:webView]; 

} 

編輯:: 添加故事板圖片

enter image description here

所以,我怎麼能做出正確的alignm嵌入顏色/鏈接是否正確?

+1

嘗試給webView = [[UIWebView alloc] initWithFrame:self.view。 **界限**];而不是幀 – Charan

+0

爲什麼'[html description]' 而不是'html'? – DAS

+0

是不是你如何傳遞字符串到webview? –

回答

3
  1. 檢查您的UIWebView尺寸是否合適。它似乎比屏幕更大。

  2. 將任何前\「在您的硬編碼字符串 如:

    NSString *testString="This is a \"Test\""; 
    

    結果

    這是一個 」測試「

希望這會有所幫助

+0

它似乎大小無關緊要:),這就是她說的哈:)我已經改變了它幾次,但它不被Xcode識別。在問題 –

+0

上添加圖片,將代碼更改爲'webView = [[UIWebView alloc] initWithFrame:self.webView.frame];'並添加\爲我工作謝謝 –