嗨,我是编程新手,目前正在构建我的第一个iOS应用程序。在此应用程序中,我在视图控制器上设置了UIWebView,并使其加载了本地htm文件。该htm文件链接到css文件(也是本地文件)。这些在应用程序内可以很好地加载,用户可以看到内容并对其进行适当的样式设置
我的问题是,我希望用户能够更改在CSS文件中设置的内容的文本颜色。有没有一种方法可以使用代码来实现?即我将有一个名为“更改颜色”的按钮,当按下该按钮时,它将编辑css文件并更改颜色,然后重新加载页面。也想对字体大小做同样的事情。
编辑CSS文件是实现此目的的最佳方法,还是还有其他更简单或更有效的方法?
任何帮助将非常感激。:)
编写一个JavaScript(/ jQuery)函数来更改html元素文本的颜色,并从Objective C触发该JS函数。这是一个解决方案,即如何从Obj C执行JS函数:
stackoverflow.com/questions/9473582/ios-javascript-bridge
[_myWebView stringByEvaluatingJavaScriptFromString:@"changeColorAndSize();"];
这是一个更改文本颜色和大小的jQuery / HTML示例:
<html>
<head>
<link href="YOUR_STYLESHEET.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="text">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
<div id="button">TouchME</div>
</body>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<!-- After including jQuery lib, you can use it, in a script -->
<script>
function changeColorAndSize(){
$("#text").css({"color":"red", "font-size":"22px"});
}
$("#button").on("click", changeColorAndSize);
</script>
但是请考虑尽可能不使用WebView,因为这是资源最密集的视图之一。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句