0

I am getting HTML from Server as NSString. I need to apply CSS on that HTML. What I tried

NSString *htmlString = @"This is an example of a lorem ipsum dolor longer question that has about twenty-two words in it, est veniam dolorum?";


NSString *htmlStringQuestion = [NSString stringWithFormat:@"<style type='text/css'>* {padding:0px; margin:0px;} div.ContentWrapper {width:100%; height:100%; text-align:center; display:table;}div.ContentArea {display:table-cell; vertical-align:middle; padding:0 5%; background:#ff0000;color: #FF0; font-size:30; font-family: Baskerville;}</style><div class='ContentWrapper'><div class='ContentArea'><p>%@</p></div></div>", htmlString];

Now its giving warning.

enter image description here

Thanks.

Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
  • You need to escape all special characters from `htmlStringQuestion` (special characters being: `/` `%` `;` etc...) – Bassem Nov 27 '15 at 05:15
  • So, If I remove these characters, It will loss essence of being CSS or HTML. As we are removing Tags – Sam Shaikh Nov 27 '15 at 05:17
  • Escaping the characters is not *removing* them. You escape them by adding a `\\` before them. Have a look at this: http://stackoverflow.com/questions/5588495/nsstring-encoding-special-characters-like and http://stackoverflow.com/questions/15026942/best-way-to-escape-special-characters-in-nsstring-used-in-a-network-protocol – Bassem Nov 27 '15 at 05:17
  • This is local String @Bassem, So I need to apply CSS, if I remove this %, it will not be functional as CSS. – Sam Shaikh Nov 27 '15 at 05:26
  • So, let me know how to make escape of width:100% . Here Objective-C consider % as adding some new NSString. – Sam Shaikh Nov 27 '15 at 05:27
  • `width: 100%` becomes `width: 100\%` Please have a look at the links I shared to better understand what escaping special characters means. – Bassem Nov 27 '15 at 05:27
  • Let me try by replacing all % with \ . Thanks. – Sam Shaikh Nov 27 '15 at 05:28
  • 1
    @Bassem No, you escape each % by adding a 2nd %. You do not add a backslash. – rmaddy Nov 27 '15 at 05:52

1 Answers1

2

You need to properly escape each % literal in the format string by making it %%. If you had any double-quotes in the format string, those would need to be escaped by prepending a backslash like \".

rmaddy
  • 314,917
  • 42
  • 532
  • 579