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];
Related
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 :)
I want to edit a text file (basically a rich text editing) like making some text bold, changing font, color etc. I have found many rich text editor programs. But i need to parse a .rtf file and and show the contents, make it possible to edit and save it back to the .rtf file.
So I am not getting any idea to read and write into a .rtf file.
I got a library for RTF Reader and RTF Writer.
https://github.com/omnigroup/OmniGroup/tree/iOS-4.x
Initially you may get many errors on building the library.
Hey I think you can add your .rtf file to a UITextView and once in the UITextView you can make some text bold, changing font, color etc.
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myTextView.text = myText;
OR
NSError *err = nil;
NSString *fileContents = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&err];
if (fileContents == nil) {
NSLog("Error reading %#: %#", filePath, err);
} else {
myTextView.text = fileContents;
}
Also please check the following link
Read/Write RTF File
Refer the answer from Load rtf or text file into UITextView iphone sdk
How to display the created file name and contents on textview? This is my code. Iam able to display the file name and its contents in NSLog but not on textview though i have given the outlet for textview. Please help me with this issue.
-(IBAction) open: (id) sender
{
// Create instance of UITextView
textView = [UITextView alloc];
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString *homeDir = NSHomeDirectory();
// Add text
textView.text= homeDir;
NSLog(#"%#", homeDir);
textfield.text= #"output will go here..";
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:#"Myfile"
ofType:#"txt"];
NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSString *s = [NSString stringWithFormat:#"myFilePath: %#, Contents of file:%#",myFilePath, myFileContents];
NSLog(#"%#", s);
textView.text= s;
}
Does the textView work at all? Have you tried:
textView.text = #"TestText"? If the textView remains empty you need to check the outlet/ check whether the textView is added to the ViewController properly. I think this should solve your problem.
There is an issue with your text view setup.
If an outlet is connected to the text view, you do not need to create it in code (which you are doing).
If you create it in code, you have to add it as a subview to the desired view (which you are not doing).
I think you forgot to add the textview in your parentview; I assume you are creating runtime textview. Just add this line in your code.
[self.view addSubview:textView];
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
i have web site that show a text and i update this text every day , i want to show this text on iphone application , how can i get this text from web site from application ?
what should i do ?
thanks
1-> you require to connect with your web server thought HTTP connection.
2-> Make the request to server.
3-> Parse server response that may contain your "Text".
For technical assistance Read below.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
I don't recommend this as the best way to obtain a string from your own web server.
This should point you in the right direction, don't expect it to compile cleanly.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
/* set headers, etc. on request if needed */
[request setURL:[NSURL URLWithString:#"http://example.com/whatever"]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
NSString *html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSScanner *scanner = [NSScanner scannerWithString:html];
NSString *token = nil;
[scanner scanUpToString:#"<h1>" intoString:NULL];
[scanner scanUpToString:#"</h1>" intoString:&token];
This will capture text from first h1 tag.
The simplest way is to create a REST API. That might sound tough but it's really easy. On the server side, create a new page which holds only the raw text. Usually it's best to keep it there in JSON/XML format, but a simple text will also work. Now from the iPhone, just contact that address and the response data will contain the text. Parsing an existing page is not something I recommend, because changing that page in the future might result in the app not working anymore.
This is a answer quite late but I think it still might help in your future. You can go into parsing the website and that is the right way to do it but I will show you how to do it a different way, this can also be used to read xml, html, .com, anything and also, .rss so it can read RSS Feeds.
Here :
This can get your first paragraph, if you request I will show you how to get the second paragraph and so on.
//This is your URL
NSURL *URL = [NSURL URLWithString:#"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
{
NSString *webString2222 = mastaString;
NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];
NSString *content2222 = [[NSString alloc] init];
//Change <p> to suit your need like <description> or <h1>
[stringScanner2222 scanUpToString:#"<p>" intoString:Nil];
[stringScanner2222 scanUpToString:#"." intoString:&content2222];
NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:#"<p>" withString:#""];
description.text = filteredTitle;
}
Title ? Same deal change the <p> to a <title> in RSS <description> and <title>.
Image ? Same deal change the <p> to what ever your RSS or website uses to get a image to find
But remember for both of them when you change the` you see the link which says stringByReplacingOccurences of you have to change that as well.
out then you have to delete this and make your code like this :
/This is your URL
NSURL *URL = [NSURL URLWithString:#"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
NLog(#"%#", string)
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
Now check your log it shows your whole website html or rss code then you scroll and read it and find your image link and check the code before it and change the String Scanner to your needs which is quite awesome and you have to change the stringByReplacingOccurences of.
Like I said images are a bit tricky when you do it with this method but XML Parsing is a lot easier ONCE you learn it , lol. If you request I will show you how to do it.
Make sure :
If you want me to show you how to do it in XML just comment.
If you want me to show you how to find the second paragraph or image or title or something just comment.
IF YOU NEED ANYTHING JUST COMMENT.
Bye have fun with code I provided, anything wrong JUST COMMENT! !!!!
:D