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];
Related
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 have this really unanswered question.. after looking in the web for this problem i tring my luck with you..
i have a UITextView that gets the string from url.
but... the textfield is cuts/hidden fron the half first row and its fixed only when i touch it.
this is the code:
- (NSString *)getMonthValues{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#",URL_ADDRESS2,URL_MONTH_ZODIAC]];
NSString *Value = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
Value = [Value stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];
}
-(void)getMonthValue{
NSString *horoscope_daily = [self getMonthValues];
[Month_horoscope setText:[NSString stringWithFormat:#"%#",horoscope_daily]];
}
can someone please explain this problem and help me fix it ?!
It is either 1) covered by other view, or 2) has a frame too small to see the whole text.
To check out the cover issue, try setting background property to [UIColor redColor], on all other visible views.
And if it is the frame issue, right after you setText, issue:
[Month_horoscope sizeToFit];
My UIDocumentsInteractionController is working as far as presenting an action sheet with a button that says "iBooks" but when I click on that button, it just dismisses and it doesn't take me to iBooks. Here's my code:
NSString *filenamePath =[NSString stringWithFormat:#"temp.%#", [[file path] pathExtension]];
NSString *docDir = [DataCenter getDocumentsDirectoryPath];
NSString *fullPath = [docDir stringByAppendingPathComponent:filenamePath];
NSURL *url = [NSURL fileURLWithPath:fullPath];
UIDocumentInteractionController *c = [UIDocumentInteractionController interactionControllerWithURL:url];
BOOL success = [c presentOpenInMenuFromBarButtonItem:buttonBack animated:YES];
What am I doing wrong?
Thanks
For those stuck at this: You don't need to set yourself up as UIDocumentInteractionController's delegate at all.
Problem was [UIDocumentInteractionController interactionControllerWithURL:url] is being autoreleased. It thought it would be retained internally by the action sheet being shown but apparently it's not.
So yea, gotta retain it until action sheet dismisses.
Try checking the UIDocumentInteractionControllerDelegate methods documentInteractionController:willBeginSendingToApplication: and documentInteractionController:didEndSendingToApplication:. If your view controller is the delegate of the document interaction controller, that should clue you in to where the problem may be.
Also, you should validate that the file you're trying to use elsewhere (a PDF I assume) is actually a what you expect it to be.
I'm going to ask this question again,I thought I had figured it out but I guess not. This is for a timer app that gets the timer info from a plist file. Everything works but I want the .plist extension not to show in my tableview. Here are two code snippets that I believe are the issue. These are in an array called NSArray *files;
self.files = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"Timers1"];
// Configure the cell.
cell.textLabel.text = [[files objectAtIndex:indexPath.row] lastPathComponent];
return cell;
Thanks for the help
NSString *text = [[files objectAtIndex:indexPath.row] lastPathComponent];
text = [text substringToIndex:[text rangeOfString:#"."].location];
cell.textLabel.text = text;
I have a UITextView in a DetailViewController with some text which is loaded from a NSMutableDictionary in the MainViewController. Here is the code for that;
- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
if ((int)index == 0) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"This is some text", #"textkey", nil];
detailViewController.myDictionary = myDictionary;
}
I have loaded the string from this into my DetailViewController with this code;
self.myText.text = [(NSMutableDictionary *)myDictionary objectForKey:#"textkey"];
Also in my viewDidLoad I have created a RightBarButton named 'Save' which I use to hide the keyboard when the viewer is done editing. I would like this button to also save the changes the viewer enters into the UITextView (as well as the original text).
This is the code for the rightBarButton;
UIBarButtonItem *saveButton =[[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(textViewDidChange:)];
self.navigationItem.rightBarButtonItem = saveButton;
Lastly I have code which envokes the textVidewDidChange and hides the keyboard. I am trying to also have it save the changes to the textView but it doesn't.
-(void)textViewDidChange:(UITextView *)textView;
{
if( textView == myText )
[myDictionary setValue:myText.text forKey:#"textkey"];
[myText resignFirstResponder];
}
Can anyone help me accomplish this. I simply want to save the changes to the UITextView back to the NSMutableDictionary. (or maybe not so simply)
I have changed the button to
`UIBarButtonItem *saveButton =[[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(didChangeValueForKey:)];
self.navigationItem.rightBarButtonItem = saveButton;`
and other code to ;
`-(void)didChangeValueForKey:(NSString *)key{
NSError *error;
[myDictionary setValue:[self myText].text forKey:#"textkey"];
[myText resignFirstResponder];
NSLog(#"%#", [myDictionary valueForKey:#"textkey"]);
}
My NSLog shows the changes but when I reopen the app they are gone, not saved. Can one save directly to the NSMutableDictionary?
I read a lot on persistent data. Thought NSData or Plist but try as I may not doing well.
Can someone suggest a good tutorial on this?
I looked at suggested link and added this (bold part) to my code.
`-(void)didChangeValueForKey:(NSString *)key{
/* NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:#"textkey"]];*/
NSError *error;
[myDictionary setValue:[self myText].text forKey:#"textkey"];
**[myDictionary writeToFile:#"textkey" atomically:YES];**
[myText resignFirstResponder];
NSLog(#"%#", [myDictionary valueForKey:#"textkey"]);
}`
As you can see I also tried getting the path using [NSHomeDirector] above and then replaced
#"textkey" with path. I can still see the changes in NSLog (either way) but there is no change when I reload the view or relaunch the app.
I have change things so that I am saving a text file of the text in the textview with the name gotten from the dictionary so that each time a different detailView is loaded depending on the image selected in the mainViewController.
This is my dictionary entry in the mainViewController;
`if ((int)index == 0) {
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
//here is where I want to access the text file name to load in the UITextView in my DetailView
#"first View Text", #"textkey2",
//This is where I give the text file of the changed text its name for each different view of the DetailView
#"first View Text", #"textkey3",nil];
detailViewController.myDictionary = myDictionary;
}`
Next is the code I use to save the changes to the textView using the UIbarbuttonright
`- (void)saveAction:(id)sender {
[self.myText2 resignFirstResponder];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:#"textkey3"]];
NSString *savedString = myText2.text;
NSLog(#"The Save file is:%#", savedString);
[savedString writeToFile:documentTXTPath atomically:YES
encoding:NSUTF8StringEncoding error:NULL];
}`
I have checked this and the file is saved in the documents folder under the name of First View Text and it does contain the changed text.
I am having problems loading the text file contents into the UITextView.
Using the code I have I get the path to the textkey2 object (First Text View) not the contents of the file.
`NSString *textName = [myDictionary objectForKey:#"textkey2"];
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *fullPath2 = [documentsDirectory2 stringByAppendingPathComponent:textName];
/* self.myText2.text = fullPath2;*/
self.myText2.text = [NSString stringWithContentsOfFile:fullPath2 encoding:NSUTF8StringEncoding error:NULL];`
I replaced this last line with the one that isn't commented out and it works fine. For anyone who want to know.
You need to implement data persistence, e.g. saving the text into permanent storage that you can retrieve again when you reload the view later on.