Đặt chiều rộng của HTML UIWebView (Set width of HTML UIWebView)


問題描述

Đặt chiều rộng của HTML UIWebView (Set width of HTML UIWebView)

Can I programmatically set the width of the HTML file that I throw into the UIWebView? My problem is that the UIWebView frame is set in a UITableViewCell, so I had to set CGRectMake to fit it in the cell. But now the size of the content is bigger than the frame. Can I set the width in the UIWebView for the HTML? 

My HTML Code: 

<div id="attachment_16505" class="wp‑caption alignnone" style="width: 460px"><img src="http://www.floorballmagazin.de/wp‑content/uploads/2013/03/20130303_schweiz.jpg" alt="Maßgeblich beteiligt ‑ Mathias Hofbauer sprang von der Strafbank und netzte zum 3:1 ein. / Foto: Hans Ulrich Muelchi, Swiss Unihockey" width="460" height="318" class="size‑full wp‑image‑16505 colorbox‑16504" /> <p class="wp‑caption‑text">Maßgeblich beteiligt &#8211; Mathias Hofbauer sprang von der Strafbank und netzte zum 3:1 ein. / Foto: Hans Ulrich Muelchi, Swiss Unihockey</p> </div> <p><em>In der Schweiz kann Wiler sein historisches Ausscheiden aus den Playoffs mit einem versöhnlichen Cupsieg kurieren. Vor über 3.000 Zuschauern besiegen die Berner Grünenmatt mit 3:1. Bei den Damen kann Sandra Dirksen ihren Titelreigen nicht fortsetzen, die Red Ants brachten eine versprechende Führung nicht über die Runden.</em></p> <p>Als Wilers Kapitän Mathias Hofbauer in 56. Spielminute auf die Strafbank wanderte, wurde es nochmal eng für den noch aktuellen Meister. Sein Unterzahlspiel überstand man aber mit Bravour und als Hofbauer wieder zurück aufs Feld durfte, dauerte es gerade mal sechs Sekunden und der WM‑Rekordscorer machte mit einem scharfen Schlenzer alles klar.</p> <p>Bei den Damen waren Deutschlands Ex‑Nationalspielerin Sandra Dirksen und ihre Red Ants auf dem Weg zum vierten Cupsieg in Folge. Rychenberg führte zu Beginn des Schlussdrittels mit 2:0, brach aber ein und unterlag Chur mit 2:5. &#8220;Wer den Pokal mit nach Hause nehmen will, muss drei Drittel gut spielen und nicht nur zwei&#8221;, bedauert Dirksen.</p> <p>&#8220;Wir hatten zwar den besseren Start, aber am Ende doch nur Silber. Fünfzehn Sekunden vor der 2. Drittelspause haben wir ein abgelenktes, unglückliches Tor kassiert, das war der Knackpunkt leider.&#8221; Dafür sei man umso motivierter für die bevorstehenden Play‑Offs. &#8220;Die Atmosphäre bei einem Cup‑Finale ist und bleibt einzigartig. Unsere Fans waren toll&#8221;, schätzt Dirksen.</p>

This is what happens in the cell:


參考解法

方法 1:

One solution is to intercept your HTML and prepend:

<html>
<head>
<style>div {max‑width: 460px;}</style>
</head>
<body>
<div>

And append:

</div>
</body>

Take a look at this question/answer (HTML Content fit in UIWebview without zooming out) from a while back. I posted some code there.

方法 2:

You could either try to set the height of the HTML contents  within your HTML code equals to UIWebView. Otherwise try this command to set the size of UIWebView fitting to the page

webView.scalesPageToFit = YES;

方法 3:

One solution can be :

Load the URL Request

NSString *urlAddress = @"your URL";
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:urlAddress]];
[self.webView loadRequest:requestObj];  
self.webView.delegate = self;

Then implement WebViewDidFinishDownload delegate to correctly set the scale factor/zoom level

‑ (void)webViewDidFinishLoad:(UIWebView *)webView
{
  CGSize contentSize = webView.scrollView.contentSize;
  CGSize viewSize = self.view.bounds.size;

  float sfactor = viewSize.width / contentSize.width;

  webView.scrollView.minimumZoomScale = sfactor;
  webView.scrollView.maximumZoomScale = sfactor;
  webView.scrollView.zoomScale = sfactor;  
}

Hope this Helps !!!

(by user1163463mprivatnsgulliverarun.s)

參考文件

  1. Set width of HTML UIWebView (CC BY‑SA 3.0/4.0)

#iphone #iOS #uiwebview #html






相關問題

iPhone 3.0 MPMoviePlayerController 在屏幕上顯示視頻播放器的等待時間長有問題嗎? (Problem with iPhone 3.0 MPMoviePlayerController long wait time to display the video player on screen?)

將 NavigationBar 背景更改為圖像 (Changing the NavigationBar background to image)

如何將以下行添加到我的 plist 文件中? (How can I add the following lines to my plist file?)

iPhone SDK:檢查 UITableView 中每個對象的 ID 號並相應地更改徽章號 (iPhone SDK: Checking the ID number for each object in a UITableView and change the badge number accordingly)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

嵌套 XML 示例? (Nested XML Example?)

提交應用程序 - 添加本地化,但我的語言未列在組合框中? (Submitting app - Add Localizations, but my language is not listed in the combo box?)

如何從移動瀏覽器直接訪問移動設備的硬件功能 (How to gain direct access to the hardware capabilities of mobile devices from mobile browsers)

Cocos2d 中的粒子碰撞檢測 (Collision detection with particles in Cocos2d)

捕捉返回按鈕導航事件 (Catching back button navigation event)

iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)

帶有 UITableView 的 UIPopoverController (UIPopoverController with UITableView)







留言討論