Save UITextView content in NSString - iphone

I'm trying to save the contents of an UITextView into a NSString which I will somehow persist later.
The problem is that the UITextView "text" property doesn't save all the rows in the textView (after touching "return"). I tried print the TextView object and the textView.text, and they're different.
Does anybody know how (after editing the textView) I can save its content into a String (or something else that I can later access and share through different views and persist in the database)?
Thanks a lot. Pretty sure it is simple, but I'm honestly not finding the solution.

NSString *textViewString = myTextView.text
ought to be what you need. But you say that the TextView object and its text property have different values? Has your delegate received – textViewDidEndEditing: or – textViewDidChange: yet?

Related

UILabel text moved

I have a UITextView in which it has a place holder text in it, however, when I tried to type in something to the UITextView it moved my UILabel text to the right a bit. Why is this? I can't seem to figure out how this could happen. Here's a video showing the issue.
I am using SSTextView from Sam Soffle's SSToolkit here for the UITextView.
All the other is pretty much standard. Can anyone tell me why is this?
Not sure if it helps, but can it be due to:
- (void)textViewDidChange:(UITextView *)textView
{
[self.textCount setText:[NSString stringWithFormat:#"%3d/142", textView.text.length]];
}
[self.textCount setText:[NSString stringWithFormat:#"%3d/142", textView.text.length]];
When you first init your TextField do you have the same formatting?
If you have set in in IB, remove it from there and set it in code with that formatting.
But maybe a simpler solution would be to justify your text to the right and place your TextField accordingly, so you wouldn't event have to deal with the place holder.
Have you log out the frame information of that UILabel to see if it's really moving?
I'm not sure the problem is that the UILabel is moving.
The moving occur when you first modify it's text. This make me think that the problem maybe something else.
How, where and when are set the text property of that UILabel may hold the key to that mystery.

How can I send characters to cursor position of UISearchBar?

I am trying to add a couple characters that are inconveniently located in the normal keyboard, and place them in a toolbar so that the user can use them just like normal keys.
Does anyone have a useable way to do this?
I found an article explaining how to do this by simulating a "Paste" operation, (remove pasteboard contents, replace with my character, paste into field, return original pasteboard contents) but my trouble is that I'm trying to do this with a UISearchBar, which seems to have no paste selector.
Update
I found a lead:
UIKIT_CLASS_AVAILABLE(2_0) #interface UISearchBar : UIView {
#private
UITextField *_searchField;
Since it is documented that there's a UITextField in a search bar, if I were to root through the searchbar's subviews and locate said text field, (assuming with 99% certainty that the text field has a delegate) would it make sense that I could "steal" the text field and make my class the delegate, then forward the messages to the original delegate once I'm done with them?
This is definitely tricky. UISearchBar doesn't give you inputAccessoryView and nor do you get selectedRange.
You can paste in a UISearchBar. If you want to get your tricky characters to the pasteboard, you could get a button to execute something such as:
[[UIPasteboard generalPasteboard] setString:#"[*]"];
and then get the user to use paste in the UISearchBar. Pretty awkward for the user though.
Rooting through the subviews to find the UITextField might work. If you do this, you'd need to grab the existing delegate and make yourself the delegate. Then your delegate would need to transmit messages on. The process is described in this stackoverflow question and answer. Potential challenges here: (a) the Apple implementation could change between iOS updates and even, though unlikely, change the delegate during the lifetime of the UISearchBar; (b) Apple might see this as using a private API and reject the app. (I don't have any hard evidence of (b), but it's something to consider.)
One approach might be to use the bookmark button. The UISearchBar delegate can detect this. You could use that to insert your special characters or offer up a menu of special character insertions. Of course, you won't know where the cursor is. But, depending on your use case, appending the special characters at the end might be OK. Perhaps this doesn't get you anything over a button on your interface that just appends something.
[[self searchBar] setText: [NSString stringWithFormat:#"%#[*]", [[self searchBar] text]]].)
Implementing your own search bar might be the best way to go as already suggested #hyperbole. I've done this successfully by adding a custom UITextField (with my own magnifying glass in the leftView slot etc.) and adding it as the titleView of my navigationBar. But, if I understand your question aright, that still won't be enough, as UITextField doesn't provide selectedRange and its delegate doesn't provide an equivalent of textViewDidChangeSelection:. You might have a go with a UITextView that is fixed to one line (with scrolling clamped down if required - it often seems to be).
Can't you simply set the text of the UISearchBar? Of course, the tricky part is to determine the cursor position. For that, you can register a UITapGestureRecognizer on the UISearchBar, determine the tap co-ordinates & calculate the cursor position using - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode or its variants.
You may also have to register a UIPanGestureRecognizer, as the user can change the cursor position by tapping, dragging & then releasing the finger.
HTH,
Akshay

Iphone, user created labels?

Usually When i want a object to be accesed by many different event calls i just declare it in the h file.
However im making a app where i need to allow the user to create UILabels and drag and drop them where they want. So i have the code to make the labels and a alertview where they change the text.
-(IBAction)AddText{
UITextField *TextView;
TextView = [[UITextView alloc] initWithFrame:CGRectMake(40.0, 40.0, 40, 40)];
[self.view addSubview:TextView];
Which works however i need the user to be able to create many textviews which each have a unique name and a way i can reference them later such as TextView.text, doing it this way always creates a object named TextView and i can only reference it from his event, otherwise its undeclared.
Im really stuck at how i would go about doing this if anyone could help it would be great!,
Thanks
You could think about adding the newly created label to a NSMutableArray or, better, to a NSMutableDictionary. This latter solution gives you the possibility of creating a dictionary in which each label can be referred to using a string key. If you put such a dictionary in your .h file you'll be fine, I think.
How about creating an NSMutableArray of NSDictionary items. The dictionary items could have a reference to the label as well as things like name, and other relevant meta information.
You can add the UITextViews to a collection of some sort, depending on how you want to retrieve them later.

Assign string to UITextView

I am trying to load text into a textview. This seems simple enough:
box.text = #"hello";
NSLog(#"Box.text contains: %#", box.text);
The problem is that this NSLog just keeps printing null.
(I have IBOutlet UITextView *box declared.) I imagine that since box.text is null, that's why nothing is actually showing up in the UITextView either.
Anyone know what's going wrong?
Thanks a lot!
If you're calling box.text from your controller's init method, your IBOutlet hasn't been loaded from the nib file yet – so box is still pointing to nil, and the text assignment is going nowhere. You'll need to wait until viewDidLoad or later to successfully access the properties of your IBOutlet.

Detect when a user clicks the paste button in a UITextView

I am having quite a issue trying to change the cut/copy/paste behavior of the UITextView.
What I want to achieve is: detect when the user has pasted some text into the UITextView. When I detect this I will then check the data and do my thing.
According to the documents, I found out about UIResponder.
So I created an simple class that inherits UITextView.
in the .m file I create 1 function called.
-(void) paste:(id)sender{
NSLog(#"paste button was pressed do something");
}
But for some reason it never seems to fire. I could get the select statement working and tracing data.
-(void) select:(id)sender
1. Is this the correct way to detect Paste in a UITextView?
2. Right now I am tracking buy how many Characters UITextView changes and if its greater than one char then I suspect that it could be a paste operation. But since the iPhone can autocomplete words eg Wedn (goes to Wednesday) its possibly not a paste operation.
In Interface Builder, I selected my textView in my NIB file, and selected its "Class Identity" to my nearly created class before and I know that this file was working as a subclass but it just would not respond to the Paste event.
thanks.
UITextView has a view that handles the cut, copy, paste. It's UIWebDocumentView. So if UITextView is the first responder, UIWebDocumentView will get it first instead of your implementation. I would like to overwrite these functions so this is very frustrating.
Same thing happens to me too. Select (and copy sometimes) gets called but paste never.
I have simulated the behavior using textViewDidChange where I always verify the difference between the current text and the previous one. If there are more letters different and the text.length is bigger than it must have been pasted.
Hope Apple will fix this.
In case somebody still needs short and easy solution I'll share mine. I use wonderfull NSObject+REResponder category from REKit project. The solution is as easy as this:
- (void)hookMessageTextFieldPasteAction
{
void (^textFieldDidPasteBlock)(id receiver, id sender) = ^(id receiver, id sender)
{
NSLog(#"paste hook");
};
[self.messageTextField respondsToSelector:#selector(paste:) withKey:nil usingBlock:textFieldDidPasteBlock];
}
Yes your above code for paste is correct according to Apple's Documentation which refers to it here.
- (void)paste:(id)sender
I suspect you are implementing it in the wrong file. You should be implementing it in a file that is part of the 1st Responder chain. Have you subclassed the UITextView or are you using a vanilla one in your ViewController?
Hmmm I think the problem is that you may need to make your UITextView subclass become the delegate in order for the delegate method to work, because it's not by the looks of things. I'll try to find how i did that before.
Okay think i found it. I think you need to do this on your subclassed UITextField class:
#interface mySpecialTextFieldClass : NSObject <UITextFieldDelegate>
{
}
Adding that onto the end should work, give it a try!
What it does is makes your subclass become an object of a type that the delegate can send a notification to...it's polymorphism, unless someone wants to correct me here :)
One last thing to try John, in the ViewController which contains the IBOutlet to the UITextView, try calling this method on the instance of the UITextView:
[myUITextView setDelegate:self];
Just to throw something completely random in there try setting it to become first responder.
[myUITextView becomeFirstResponder];
This is called programming in the dark kiddies! And is a very bad way to program, but I admit I do it and sometimes it pays off :P lol.