以下是您需要做的:
在拥有 web 视图的 UI 控制器中,将其设置为 UIWebViewDelegate
。
然后在设置要加载的 URL 时,将控制器作为代理进行设置:
NSString *urlAddress = @"http://dl.dropbox.com/u/50941418/2-build.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
最后,要实现webViewDidFinishLoad:
方法来正确设置缩放级别:
此选项适用于 iOS 5.0 及以上版本
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
CGSize contentSize = theWebView.scrollView.contentSize;
CGSize viewSize = theWebView.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
}
选项B,您可以尝试修改HTML(此示例可完成任务,但从HTML解析角度来看不太完美。我只是想阐明我的观点。它确实适用于您的示例,而且可能适用于大多数情况。40的缩进可能可以通过编程自动检测,我没有尝试研究这一点。
NSString *urlAddress = @"http://dl.dropbox.com/u/50941418/2-build.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSString *html = [NSString stringWithContentsOfURL:url encoding:[NSString defaultCStringEncoding] error:nil];
NSRange range = [html rangeOfString:@"<body"];
if(range.location != NSNotFound) {
// Adjust style for mobile
float inset = 40;
NSString *style = [NSString stringWithFormat:@"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
html = [NSString stringWithFormat:@"%@%@%@", [html substringToIndex:range.location], style, [html substringFromIndex:range.location]];
}
[webView loadHTMLString:html baseURL:url];
webView.scalesPageToFit = YES;
<meta name="viewport" content="width=device-width; minimum-scale=1.0; maximum-scale=1.0; user-scalable=no">
<body onorientationchange="updateOrientation();">
使用JavaScript中相应的功能:
function updateOrientation() {
if(Math.abs(window.orientation)==90)
// landscape
else
// portrait
}
编辑:
根据您的页面源代码,看起来您是用网页编辑器制作的,是吗?
好的,我明白了。您的主要div宽度为600像素。iPhone屏幕分辨率为320x480。600>320,因此它超出了屏幕界限。
现在,让我们进行一些简单的操作:
320 / 600 = 0.53
480 / 600 = 0.8
所以你想最小缩小0.5倍,最大缩小0.8倍。让我们更改视口:
<meta name="viewport" content="width=device-width; minimum-scale=0.5; maximum-scale=0.8; user-scalable=no">
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
webview方法。 - MartinSwift 3:
使用此扩展可根据 webview 的大小调整 webview 内容的大小。
extension UIWebView {
///Method to fit content of webview inside webview according to different screen size
func resizeWebContent() {
let contentSize = self.scrollView.contentSize
let viewSize = self.bounds.size
let zoomScale = viewSize.width/contentSize.width
self.scrollView.minimumZoomScale = zoomScale
self.scrollView.maximumZoomScale = zoomScale
self.scrollView.zoomScale = zoomScale
}
}
webViewOutlet.resizeWebContent()
@implementation UIWebView (Resize)
- (void)sizeViewPortToFitWidth {
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=%d;', false); ", (int)self.frame.size.width]];
}
@end
document.querySelector('meta[name=\"viewport\"]')
。 - Mike Richards您可以从HTML生成NSAttributedString
(在后台执行):
@implementation NSAttributedString (Utils)
+ (void)parseHTML:(NSString *)html withCompletion:(void (^)(NSAttributedString *completion, NSError *error))completion
{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
NSError * __autoreleasing *error = nil;
NSAttributedString *result = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:error];
NSError *safeError = (error != nil) ? *error : nil;
dispatch_sync(dispatch_get_main_queue(), ^(void){
completion(result, safeError);
});
});
}
@end
并通过 UITextView
实例显示它:
[NSAttributedString parseHTML:htmlString withCompletion:^(NSAttributedString *parseResult, NSError *error) {
bodyTextView.attributedText = parseResult;
}];
然而,使用这种方法可能会破坏一些布局特性。