0

Hi I'm new to programming and am currently building my first iOS app. In this app, I have a UIWebView set up on a view controller and have it loading a local htm file. This htm file is linked to a css file (also local). These load fine within the app and the user can see the content and it is styled appropriately

The issue I have is this, I want the user to able to change the text colour of the content which is set within the css file. Is there a way to achieve this using code? i.e. I would have a button called "change colour" and when pressed it would edit the css file and change the colour and then reload the page. Also would like to do the same thing with font size.

Is editing the css file the best way to achieve this or are there other easier or better methods?

Any help would be much appreciated. :)

jcooper_82
  • 97
  • 1
  • 1
  • 8
  • Write a JavaScript(/jQuery) function to change the html element text colour, and fire that JS function from Objective C. Here is a solution, how to perform a JS function from Obj C: http://stackoverflow.com/questions/9473582/ios-javascript-bridge [_myWebView stringByEvaluatingJavaScriptFromString:@"myFunc();"]; – danieltmbr Aug 24 '14 at 09:32
  • Thanks for your help! I've looked through the link but I'm still a little unsure as to what I should put in @"myFunc()" Do you know what should be put in there to say change the colour of the body rule? – jcooper_82 Aug 26 '14 at 13:30

1 Answers1

0

Write a JavaScript(/jQuery) function to change the html element text colour, and fire that JS function from Objective C. Here is a solution, how to perform a JS function from Obj C:

stackoverflow.com/questions/9473582/ios-javascript-bridge

[_myWebView stringByEvaluatingJavaScriptFromString:@"changeColorAndSize();"];

And here is a jQuery/HTML example to change text colour and size:

<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>

But consider not using WebView if it's possible, because that is one of the most resource-intensive view.

danieltmbr
  • 1,022
  • 12
  • 20