Issue with font parsing - html

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

Related

how to change particular string from a paragraph in ios?

<a idref="_5AD480AC_7D6C_4AA6_B003_F1C5F38F4D15"
sectionid="n1e14b600d6dec639" internal="true" type="text"
slug="histologie/quergestreifte+muskulatur+histologie" link="" ui-
sref="main.learn.content({slug:
'histologie/quergestreifte+muskulatur+histologie', '#':
'_5AD480AC_7D6C_4AA6_B003_F1C5F38F4D15'})" webtrends-track-on-click="
{'WT.i_module': 'Textlink - Intern'}" >
ultrastrukturelle Aufbau der Myofilamente
</a>
I have above content as response. I just want to change that string to below.
How to get:
href="/lernmodule/histologie/quergestreifte+muskulatur+histologie#_5AD480AC_7D6C_4AA6_B003_F1C5F38F4D15"
from the above string?
If the format of your response is specific like you shown in your question then below code will work for you:
NSString *match = #"internal";
NSString *postTel;
NSString *preTel;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:match intoString:&preTel];
[scanner scanString:match intoString:nil];
postTel = [str substringToIndex:scanner.scanLocation];
NSString*requiredString=[[postTel substringFromIndex:3] stringByReplacingOccurrencesOfString:#"internal" withString:#""];
strContent=[strContent stringByReplacingOccurrencesOfString:#"', '#': '" withString:#"'#"];
strContent=[strContent stringByReplacingOccurrencesOfString:#"'})\" web" withString:#"\" web"];
strContent=[strContent stringByReplacingOccurrencesOfString:#"({slug: '" withString:#" \" href=\"/lernmodule/"];
strContent=[strContent stringByReplacingOccurrencesOfString:#"\"\"#" withString:#"#"];

How to change a Html text color dynamically in Objective C

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

HTML with <blockquote> tags in UITextView

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:

Html parsing using hpple

i need your help.
This is the first time i try to parse HTML and i am running through some problems. I followed Rays Tutorial about HTML parsing. He uses hpple. The file i want to parse is a lot more complicated than his. I want to excract some variables through the code below:
<div id="status">
<div id="loading" style="display:none">Error:<br />Connection to demo board was lost.</div>
<div id="display">
<span style="float:right;font-size:9px;font-weight:normal;padding-top:8px;text-indent:0px">(click to toggle)</span>
<p>LEDs:<br /><span class="leds">
<!-- <a id="led7" onclick="newAJAXCommand('leds.cgi?led=7');">•</a>
<a id="led6" onclick="newAJAXCommand('leds.cgi?led=6');">•</a>
<a id="led5" onclick="newAJAXCommand('leds.cgi?led=5');">•</a>
<a id="led4" onclick="newAJAXCommand('leds.cgi?led=4');">•</a>
<a id="led3" onclick="newAJAXCommand('leds.cgi?led=3');">•</a>
<a id="led2" onclick="newAJAXCommand('leds.cgi?led=2');">•</a> -->
<a id="led1" onclick="newAJAXCommand('leds.cgi?led=1');">•</a>
<!-- <a id="led0">•</a> -->
</span></p>
<p>Buttons:<br />
<!-- <span id="btn3">?</span>
<span id="btn2">?</span>
<span id="btn1">?</span> -->
<span id="btn0">?</span></p>
<p>Potentiometer: <span id="pot0" style="font-weight:normal">?</span></p>
<p>Temperature: <span id="temp0" style="font-weight:normal">?</span></p>
</div>
So far i can get LEDs, Buttons, Potentiometer and Temperature. I can not get their values . (the values of those specific 4 fields).
I am using the code below:
- (void)loadTutorials {
// 1
NSURL *tutorialsUrl = [NSURL URLWithString:#"http://192.168.0.112/"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];
// 2
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
// 3
NSString *tutorialsXpathQueryString = #"//div[#id='display']/p";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
// 4
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
// 5
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
NSLog(#"Object=%#",element);
// 6
tutorial.title = [[element firstChild] content];
// 7
tutorial.url = [element objectForKey:#"???"]; //???
}
// 8
_objects = newTutorials;
[self.tableView reloadData];
}
I suspect that the problem is the objectForKey: But i am not sure. I tested all sort of keys i could imagine. Any help would be most welcome.

variable font size in UIWebView

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