ios - 如何在 Objective-C 中编辑 CSS 文件

标签 ios css objective-c uiwebview edit

大家好,我是编程新手,目前正在构建我的第一个 iOS 应用程序。在这个应用程序中,我在 View Controller 上设置了一个 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,因为这是最耗费资源的 View 之一。

关于ios - 如何在 Objective-C 中编辑 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25457395/

相关文章:

objective-c - 使用 prepareForSegue 在 View 之间发送图像

css - 如何使 DIV (SIDEBAR) 延伸到滚动页面的末尾?

javascript - Resizer 扩展和最小化 div

ios - 集成 Nuance Dragon Mobile 时架构 x86_64 的 undefined symbol

objective-c - 应用内购买更新应用程序

ios - TransitionFromView 移除之前的 View

ios - 即使计时器在视觉上已经停止,NSTimer 也会继续计数

css - 查找用户成员(member)计划 WooCommerce

ios - 在 iOS 中检索短信文本

ios - viewDidAppear 之后没有调用 didShowViewController