android 如何在程序中设置webview大小?

android 如何在程序中设置webview大小?,第1张

android 如程序设置webview大小的方法为:

1、冲模android自枝羡带的五种字体大小:

SMALLEST(50%),

SMALLER(75%),

NORMAL(100%),

LARGER(150%),

LARGEST(200%)

代码:webSettings.setTextSize(TextSize.LARGER)

2、android3。0以下的系统猛判拍可以用下面的代码 :

public static void setScaleVsalue(View view, double size) {

Class classType

Method method = null

try {

classType = WebView.class

for (Method item : classType.getDeclaredMethods()) {

if (item.getName().equals("setNewZoomScale")) {

method = item

}

}

if (method != null) {

method.setAccessible(true)

method.invoke(view, new Object[] { (float) (size / 100.0),

true, true })

}

} catch (SecurityException e) {

e.printStackTrace()

} catch (IllegalAccessException e) {

e.printStackTrace()

} catch (IllegalArgumentException e) {

e.printStackTrace()

} catch (InvocationTargetException e) {

e.printStackTrace()

}

}

MyWebView.setScaleValue(mMyWebView,textSize)

1.设置字体大小

[objc] view plain copy print?

-(void)webViewDidFinishLoad:(UIWebView *)webView

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '40%'"]//修改百分比即可

2.设置webView内容宽度纤桐运等于屏幕宽轮册度显示,设置毁梁webView的缩放效果

[objc] view plain copy print?

NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=self.view.frame.size.width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\""]

[webView stringByEvaluatingJavaScriptFromString:meta]//(initial-scale是初始缩放比,minimum-scale=1.0最小缩放比,maximum-scale=5.0最大缩放比,user-scalable=yes是否支持缩放)

方法1:扰大获取webview中scrovllview的contentsize进行设置

-(void)webViewDidFinishLoad:(UIWebView

*)webView{

CGFloat

webViewHeight=[webView.scrollView

contentSize].height

CGRect

newFrame

=

webView.frame

newFrame.size.height

=

webViewHeight

webView.frame

=

newFrame

}

方法2:执行js语句罩雀 直接获取html文档的dom高度

-(void)webViewDidFinishLoad:(UIWebView

*)webView{

CGFloat

webViewHeight=

[[webView

stringByEvaluatingJavaScriptFromString:

@"document.body.offsetHeight"]floatValue]

// CGFloat webViewHeight= [[webView

stringByEvaluatingJavaScriptFromString:

@"document.body.scrollHeight"]floatValue]

CGRect

newFrame

=

webView.frame

newFrame.size.height

=

webViewHeight

webView.frame

=

newFrame

}

方法3.先将UIWebView的高度设为最小缓闷竖,然后再使用sizeThatFits就会返回刚好合适的大小

-(void)webViewDidFinishLoad:(UIWebView

*)webView{

CGSize

actualSize

=

[webView

sizeThatFits:CGSizeZero]

CGRect

newFrame

=

webView.frame

newFrame.size.height

=

actualSize.height

webView.frame

=

newFrame

}

方法4.遍历webview子视图 获取UIWebDocumentView高度即实际高度

Objective-C

-(void)webViewDidFinishLoad:(UIWebView

*)webView{

CGFloat

webViewHeight

=

0.0f

if

([webView.subviews

count]

>

0)

{

UIView

*scrollerView

=

webView.subviews[0]

if

([scrollerView.subviews

count]

>

0)

{

UIView

*webDocView

=

scrollerView.subviews.lastObject

if

([webDocView

isKindOfClass:[NSClassFromString(@"UIWebDocumentView")

class]])

{

webViewHeight

=

webDocView.frame.size.height//获取文档的高度

webView.frame=

webDocView.frame

//更新UIWebView 的高度

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12549012.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存