I have html file which i am loading through below code
NSURL *htmlString = [[NSBundle mainBundle]URLForResource: #"index2" withExtension:#"html"];
NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:htmlString options:#{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
UITextView *textView1 = [[UITextView alloc] initWithFrame:CGRectMake(20,20,self.view.frame.size.width,self.view.frame.size.height)];
textView1.attributedText=stringWithHTMLAttributes;
The problem here is that textview is not loading css file ,
does NSAttributedString string support CSS ?
If yes then please help me how to do it
Thanks In advance :)
Related
I have created a UIWebView and used a HTML file to display some contents. But when I run it instead of showing the contents only the whole HTML file coding is coming in the WebView. Please help and tell me what is wrong.
UIWebView *ingradients= [[UIWebView alloc]init];
[ingradients setFrame:CGRectMake(10, 170, 300, 300)];
[ingradients loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"htmlfile" ofType:#"html"]isDirectory:NO]]];
ingradients.delegate=self;
[self.view addSubview:ingradients];
My htmlfile.html contains
<html>
<body>
<p><strong>Ingredients</strong></p>
</body>
</html>
Instead of showing "Ingredients" in bold its showing the whole coding of htmlfile.html
In Your code you alway contain HTML code because your request always return file htmlfile with extantion .html
If you want to get specific value from HTML content you need to Parce HTML content by using Hpple. Also This is documentation with exmple that are use for parse HTML content.
In your case you use: (by using Hpple)
TFHpple *dataParser = [TFHpple hppleWithHTMLData:placesData];
// name of place
NSString *XpathQueryString = #"//p/strong";
NSArray *listOfdata= [dataParser searchWithXPathQuery: XpathQueryString];
That's weird, I have similar code for this and html is rendered as rich text but not as plain text (like you have), the only difference I have is using fileURLWithPath: but not fileURLWithPath:isDirectory:. Here's my code:
NSString *localFilePath = [[NSBundle mainBundle] pathForResource:#"about" ofType:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localFilePath]];
[_aboutWebView loadRequest:localRequest];
Maybe you have some issues with file encoding, but as far as I guess, that should not be the case.
Try this code:
- (NSString *) rootPath{
return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
}
- (NSString *) pathFoResourse : (NSString *) resourseName ofType: (NSString *)type{
NSString *path = [[MMSupport rootPath] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.%#", resourseName, type]];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
path = [[NSBundle mainBundle] pathForResource:resourseName ofType:type];
}
NSLog(#"**path:%#**", path);
return path;
}
- (void) loadDataToWebView : (CGRect) frame{
NSString *htmlString = [NSstring stringWithContentsOfFile:[MMSupport pathFoResourse:#"filename" ofType:#"html"] encoding:NSUTF8StringEncoding) error:nil];
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
[webView loadHTMLString:htmlString baseURL:nil];
}
In my application I am loading HTML String which is parsed from the json file the string already contain font color and font size but I need to change the color and size of the font. To replace that I have already use the followings code but its not working
NSString *webString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",
textFontSize];
[web stringByEvaluatingJavaScriptFromString:webString];
Is there is any other way to change it.If yes means please suggest me the answer.
try this,
[webView loadHTMLString:[NSString stringWithFormat:#"<div id ='foo' align='justify' style='font-size:14px; font-family:helvetica; color:#ffffff';>%#<div>",yourString] baseURL:nil];
it may helps you....
You can try this java script in to your webview's delegate webViewDidFinishLoad method
NSString *setTextSizeRule = [NSString stringWithFormat:#"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
NSString *changeColor = [NSString stringWithFormat:#"addCSSRule('html, body, div, p, span, a', 'color: #000000;')"];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView stringByEvaluatingJavaScriptFromString:changeColor];
You can do so many changes using java script in webview's did finish load method.
Let me know if you still able to not solve
My app needs to read .rtf file from URL. I am going to read it as
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#.rtf",_URL]];
NSError* error;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
Now when I load this text in UITextView. It gives me tags with { and / characters with the original file text. Please guide me that how can i read the right text which did not include any brackets and html data.
.rtf file always contains Tags with itself for formating text color alignment and other properties..
please open that .rtf file in to text edit and convert all text part of that file to simple text.
then your existing code will work.
else instead of taking the UItextView open it in UIWebview.
hope it will be helpfull for you.
You can use RTF in a UITextView by first transforming it into an NSAttributedString and then setting the value of the UITextView's attributedString property to the NSAttributedString.
NSAttributedString *stringWithRTF = [[NSAttributedString alloc] initWithFileURL:rtfDoc options:#{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} documentAttributes:nil error:nil];
// Instantiate UITextView object
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20,20,self.view.frame.size.width,self.view.frame.size.height)];
textView.attributedText=stringWithRTF;
[self.view addSubview:textView];
I am using webview to display Data from RSS Feed Can I change color of WebView to Black From white.
I have declare outlet in xib file named RemainingRssViewController
and i am diplaying it from root view controller the code is
NSString *htmlstring =[[blogEntries objectAtIndex: blogEntryIndex] objectForKey: #"description"];
NSURL *baseUrl;
baseUrl = [[NSURL alloc]initWithString:feedurl];
[anotherViewController.maintext loadHTMLString:htmlstring baseURL:baseUrl];
//maintext is the UIWebVeiw outlet in anotherViewController.
Ok, looks good.
With this code:
NSString *htmlstring =[[blogEntries objectAtIndex: blogEntryIndex] objectForKey: #"description"];
You set up an HTML string to go in the UIWebView. However, it's not actually HTML, because right now it's just text. To solve this, we'll instead make the "htmlstring" to be styled HTML with the text from the XML feed.
For example:
NSString *textString = [[blogEntries objectAtIndex: blogEntryIndex] objectForKey:#"description"];
NSString *htmlstring = [NSString stringWithFormat:#"<html><head><style type='text/css'>body { color:#FFFFFF; background-color: #000000; }</style></head><body>%#</body></html>",textString];
Then, when the "htmlstring" is placed inside the UIWebView, it will be styled as you would like it to be.
I wanted an entire page of text(multiple lines) in my settings similar to the one on iphone:
settings->general settings->About->Legal
I have not been able to do so. I tried entering title in PSGroupSpecifier, it gives me multiple lines but with a gray background. i wanted a white background.
Any help would be highly appreciated.
Thanks in advance
Use a UIWebView and store your page as a HTML file in your resources.
// Create the UIWebView
UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
// Add the UIWebView to our view
[self.view addSubview:webView];
// Load the HTML document from resources
NSString *pagePath = [[NSBundle mainBundle] pathForResource:#"legal" ofType:#"html"];
NSURL *pageURL = [NSURL fileURLWithPath:pagePath];
NSURLRequest *pageReq = [NSURLRequest requestWithURL:pageURL];
[webView loadRequest:pageReq];