Having the following html string to load in a uiwebview, i need to set the font-size as a variable value :
NSString * htmlString = [NSString stringWithFormat:#"\
<html>\
<style type='text/css'>\
hr{ height: 0;border-bottom: 2px dotted #000;}\
.Prim{color:#000000; direction:ltr;font-size:%d;font-weight:bold;}\
.Def_en{direction:ltr;font-size:%d}\
.Def_ar{direction:rtl;font-size:%d}\
</style>\
<table width='100%%'>\<tr>\<td>\
<body>\
<p style = 'font-size:%dpx;'> %# <\p>\
</td></tr><tr><td><br><br></td></tr><tr><td align ='center'><br><img src='%#' width='60%%' \><br></td></tr></body>\
</table>\
</html>",textFontSize , authorNAme , cachePath];
This code is how i imagined it to be but it didn't work..what should i change in order of giving the font-size attribute the value of a variable textFontSize ?Thank you in advance
i actually found the problem and it was as simple as it can be...here's the code :
NSString * htmlString = [NSString stringWithFormat:#"\
<html>\
<style type='text/css'>\
hr{ height: 0;border-bottom: 2px dotted #000;}\
.Prim{color:#000000; direction:ltr;font-size:%d;font-weight:bold;}\
.Def_en{direction:ltr;font-size:%d}\
.Def_ar{direction:rtl;font-size:%d}\
</style>\
<table width='100%%'>\<tr>\<td>\
<body>\
<p style = 'font-size:%dpx;'> %# <\p>\
</td></tr><tr><td><br><br></td></tr><tr><td align ='center'><br><img src='%#' width='60%%' \><br></td></tr></body>\
</table>\
</html>",textFontSize+12,textFontSize,textFontSize,textFontSize,authorNAme , cachePath];
Related
I have a html page, My requirement is in my Objective C code I need to find a text for example in the below example I have "Color Change" in <p> tag, Once I find the text, I need to change the <p> tag color value, How can we achieve it.
> <!DOCTYPE html> <html> <body>
>
> <h1 style="color:blue;">This is a heading</h1> <p
> style="color:red;">Color Change</p>
>
> </body> </html>
if you are using local html file
then this code might be helpful for you..
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSString* text = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSLog(#"%#",htmlFile);
NSLog(#"Hello");
[self._webview loadHTMLString:[NSString stringWithFormat:#"<html><body bgcolor=\"#000000\" text=\"#FFFFFF\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%#</body></html>", text] baseURL: nil];
here in the below code color:#fff tag use for text color #fff use black color
NSString *webStr =#"Your text use";
[self._webview loadHTMLString:[NSString stringWithFormat:#"<div id ='foo' align='left' style='line-height:18px; float:left;width:300px; font-size:13px;font-family:helvetica;background-color:transparent; color:#fff;>%#<div>",webStr] baseURL:nil];
let me know , is this helpful or not for you
In the end, setTextColor: is the answer, there's an important detail missing from the earlier answers: To get this to work in iOS 8, I had to set the color =after= I set the text.
- (void)viewDidLoad
{
[super viewDidLoad];
_myTextView.textColor = [UIColor whiteColor];
_myTextView.text = #"hai hello hello..."
I would really appreciate if anyone could help me to solve an issue. I need to get an attributes string from the html string I've got which contains text patterns without font style or size. I actually want to put my specific font size and style options to that piece of text.
<body>
<div>No font style <span style="font-size: 14px;"><span style="font-family: verdana, helvetica, sans-serif;">And verdana 14 <span style="font-size: 18px;">And Verdana 18</span></span></span></div>
NSAttributedString* attrStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:#{...} documentAttributes:nil error:&error];
Please try
NSMutableString *formattedContentString = [NSMutableString stringWithFormat:#"<style>*{font-family:%#;} !important</style> %#", #"Helvetica", htmlString];
Update:
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:contentString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Your Font" size:20], NSFontAttributeName, nil]];
I'm using WKWebView loading html string, some end of html string have a few of ugly image links, i want to hide them.
The css use to hide image, but not works.
.article img[src* = "/smilies/"],
.article img[src* = ".feedburner.com/~ff/"],
.article img[src* = ".feedburner.com/~r/"],
.article img[src* = ".feedblitz.com/"]
{
display: none;
}
The sample html string with feedburner src i want to hide :
<div>
<img src="http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk" border="0">
</div>
A quick and dirty way to achieve this is by using regular expressions. Mind you that this is really not ideal for long HTML files as it is not as efficient as a real HTML parser.
// The HTML you posted
NSString *HTML = #"<div>\n\t<img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\" border=\"0\">\n</div>";
// A string containing source of the images that you want to delete
NSString *source = #"http://feeds.feedburner.com/~ff/";
// Builds a pattern that matches the tags of the images you want to delete
NSString *pattern = [NSString stringWithFormat:#"<img src=\"%#.+?>", source];
// The actual delete operation
NSString *cleanHTML = [HTML stringByReplacingOccurrencesOfString:pattern
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, HTML.length)];
// Do what you want with the cleaned HTML (display it, ...)
NSLog(#"%#", cleanHTML);
I want to display HTML text with <blockquote> tags in the UITextView.
example of HTML:
<div>
<blockquote class="uncited">
<div>
<cite>Nick:</cite>
<blockquote class="uncited">
<div>
<cite>Tom:</cite>censored<br>
Hello
</div>
</blockquote>
<p>World</p>
</div>
</blockquote>
<p>Text</p>
</div>
I use the following code to display HTML text in the cell:
UITextView *textView = (UITextView*)[cell viewWithTag:100];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[thisComment.htmlText dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;
But in the UITextView it's looks like plain text. I want to get it: sample
How can i do this? What kind of libraries should i use? i think about HTML -> Markdown -> NSAttributedString -> TextView
Apple's HTML to AttributedText parser is excellent with CSS so you can actually write it like you'd do for a raw web client.
let parsedCommentHTML = html.replacingOccurrences(of: "<blockquote>\n", with: "<blockquote>\n<k style=\"color:#ccc; font-size: 2em; font-family: 'Copperplate'\">“</k>")
let blockQuoteCSS = "\nblockquote > p {color:#808080; display: inline;} \n blockquote { background: #f9f9f9;}"
let pCSS = "p {margin-bottom: 0px;}"
let cssStyle = "\(blockQuoteCSS)\n\(pCSS)\n"
return try NSAttributedString(data: ("<html><head><style>\(cssStyle)</style></head><span style=\"font-family: HelveticaNeue-Thin; font-size: 17\">\(CONTENT)</span></html>").data(using: String.Encoding.unicode)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
This produces a nice (in my opinion) looking quote:
I want to set UIWebView with htmlstring and I have problem with an image:
...
<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="625" />
...
I want to replace height and width to 200 and 320.
I'm using
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"width=\"" withString:#"width=\"320];
then I've got result :
<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="320625" />
How to delete the number after the width from htmlString or what is the right way to replace the width to 320px?
Try this using NSScanner:
NSScanner *theScanner;
NSString *subStrng =nil;
theScanner = [NSScanner scannerWithString:htmlString];
[theScanner scanUpToString:#"width" intoString:NULL] ;
[theScanner scanUpToString:#" " intoString:&subStrng] ;
htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%#", subStrng] withString::#"width=\"320\""];
NSLog("%#",subStrng);
Try this:
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"width=\"625" withString:#"width=\"320];