0

Just like the title says... How can I do that? I want the user to enter lots of text, and then somehow minimize the keyboard and go back to interacting with other objects in that view. However, they may need the "enter" key available for formatting their text while typing. Any suggestions?

Anil suggested a link that included the following code:

(void)textViewDidBeginEditing:(UITextView *)textView {

[self createInputAccessoryView];
[textView setInputAccessoryView:_inputAccessoryView];
self.myTextView = textView;  }

-(void)createInputAccessoryView {

_inputAccessoryView = [[UIToolbar alloc] init];
_inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
[_inputAccessoryView sizeToFit];

_inputAccessoryView.frame = CGRectMake(0,_collageView.frame.size.height - 44, _collageView.frame.size.width, 44);

UIBarButtonItem *fontItem = [[UIBarButtonItem alloc] initWithTitle:@"Font"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self action:@selector(changeFont:)];
UIBarButtonItem *removeItem = [[UIBarButtonItem alloc] initWithTitle:@"Remove"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self action:@selector(removeTextView:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil
                                                                          action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                             style:UIBarButtonItemStyleDone
                                                            target:self action:@selector(dismissKeyBoard:)];

NSArray *items = [NSArray arrayWithObjects:fontItem,removeItem,flexItem,doneItem, nil];
[_inputAccessoryView setItems:items animated:YES];
[_myTextView addSubview:_inputAccessoryView];
}

However, i am a bit behind the curve conceptually...Where do I put the reference to my TextView, the .h or .m file? I don't see how...

-(void)textViewDidBeginEditing:(UITextView *)textView {

...links to my actual TextField. Also, xcode is throwing an error for "_inputAccessoryView" saying that is isn't defined anywhere. Your help is very much appreciated!

jake9115
  • 3,964
  • 12
  • 49
  • 78

2 Answers2

2

Implement the UITextViewDelegate method to resign the first responder of the text view

- (void)textViewDidEndEditing:(UITextView *)textView
 {
  [textView resignFirstResponder];
 } 

If you want to dismiss the keyboard while typing, use input accessory view of UITextField. Add a cancel button to the keyboard.
Check this question
How to add input accessory view

EDIT

What you have to do is (You can do all these things in viewDidLoad)

UIToolbar *inputAccessoryView = [[UIToolbar alloc]init]; // Create one input accessory view, tool bar will be easy for you  
inputAccessoryView.frame = CGRectMake(0,self.view.frame.size.height - 44, self.view.frame.size.width, 44);

// Add required buttons
UIBarButtonItem *fontItem = [[UIBarButtonItem alloc] initWithTitle:@"Font"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self action:@selector(changeFont:)];
UIBarButtonItem *removeItem = [[UIBarButtonItem alloc] initWithTitle:@"Remove"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self action:@selector(removeTextView:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil
                                                                          action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                             style:UIBarButtonItemStyleDone
                                                            target:self action:@selector(dismissKeyBoard:)];

NSArray *items = [NSArray arrayWithObjects:fontItem,removeItem,flexItem,doneItem, nil];
[inputAccessoryView setItems:items animated:YES];

//You should create an outlet for the text view before doing this 
[self.textView setInputAccessoryView:inputAccessoryView];
Community
  • 1
  • 1
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • I'm sorry for being bothersome, but I followed your links and still am quite confused how to implement the input accessory view. Please see my revised question, and maybe help me make this code work! – jake9115 May 07 '13 at 04:42
  • Your code should work.. create an out;et for the text view then add [self.textView setInputAccessoryView:inputAccessoryView]; in createAccessoryView itself – Anil Varghese May 07 '13 at 04:58
  • See my edit.. Do not add it as sub view. Just set the inputAccessoryView – Anil Varghese May 07 '13 at 05:00
  • thanks so much for your help so far, but after using your code I see the keyboard with the buttons, but when I click on the buttons, the app crashes! I get the following error msg: – jake9115 May 07 '13 at 16:55
  • DataFile: file open error: /var/mobile/Library/Keyboard/dynamic-text.dat, (Permission denied) 2013-05-07 12:47:29.205 HealthTry2[42902:907] -[ThirdViewController dismissKeyBoard:]: unrecognized selector sent to instance 0x1ed6dac0 2013-05-07 12:47:29.206 ExampleApp[42902:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ThirdViewController dismissKeyBoard:]: unrecognized selector sent to instance 0x1ed6dac0' *** First throw call stack: libc++abi.dylib: terminate called throwing an exception – jake9115 May 07 '13 at 16:57
  • To add a bit more info, I linked my ViewText field to the viewcontroller's header file, and then synthesized it in the .m file. I also used this as my last line of code as per your suggestion: [self.questionsTextView setInputAccessoryView:inputAccessoryView];, where questionsTextView is the name of my TextView object in the .h file – jake9115 May 07 '13 at 17:01
  • Reason for the crash is you are sending [ThirdViewController dismissKeyBoard:]. I think dismissKeyboard is the action for your done button. Did you implement that method??? – Anil Varghese May 08 '13 at 04:07
2

You can add a UIToolBar above keyboard. and then add a done UIBarBtttonItem on toolbar. So whenever user press done button then call a method and resign your textview.

#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )

Ex. : In your ViewDidLoad Method add this code

float y = IS_IPHONE_5:308:220
         toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,y,320,44)]; 
                [toolBar setBarStyle:UIBarStyleBlackOpaque];
                UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonTouched:)];

And when textViewShouldBeginEditing delegate will be called show your toolbar

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [toolBar setHidden:NO];
    return YES;
}

    #pragma mark---
    #pragma mark---IBAction Methods

    -(IBAction)doneButtonTouched:(id)sender
    {
        [YOUR_TEXTVIEW resignFirstResponder];
        [toolBar setHidden:YES];
    }

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61