How to dismiss a keyboard from textview programmatically - iphone

Is there any way to hide or do not show the keyboard without calling resignFirstResponder in iOS? I know different method to hide keyboard, first one is to make the textview non editable, or using
- (BOOL)textFieldShouldEndEditing:(UITextView *)textview
{
[textview resignFirstResponder];
}
but in my case these method will not be works out, becz I just want to dismiss or hide the keyboard only, but need the editing in textview. I have a textview which contains some text, when I tap the sentence it need to be selected, I have done that selection part nicely, but when I write above keyboard dismissal method in my project, the selection of the tapped sentence doesn't appear,.is there any method to o this.

Textfield's selection will remain only when you are in editing mode. When you resign your first responder your selection disappears too.
To avoid bringing keypad in editing mode, you can change the input view. For example , look at the code,
textview.inputView = [[UIView alloc] init];
This brings a custom input view and it is not visible.

Add
self.textView.editable = NO;
Try this
NSRange selection = [yourTextView.text rangeOfString:yourTextView.text];
if( selection.location != NSNotFound ){ yourTextView.selectedRange = selection; }
It will remove the keyboard from the textview. Hope it helps.

Related

Hide and show a view

I have small view with a UITextField in it. I want to make the view hidden by default. when a button clicked it should show the view and the elements in main view below this sub view must be scrolled down. Any idea? I'm attaching the screenshots
Set the UITextView as hidden from InterfaceBuilder (you can set it from code too).
After this attach this action handler to the UIButton.
- (IBAction)showTextBar
{
[textView setHidden:False];
return;
}
This will show the textBar. If you want to show some other elements then you can add them in this method. Also you can make this method like a toggle. click once to show the elements, click again to hide them.
Inside the action u can also use
textView.hidden = NO;
As for the scrolling use a UIScrollView something like
[_scrollView setContentOffset:CGPointMake(0,_textView.center.y+168) animated:YES];
//Alternate between hide and show when button is clicked
-(IBAction)showTextBar
{
if(textview.hidden)
{
[textView setHidden:False];
}
else
{
[textView setHidden:True];
}
}

display two UIViews and search display controller in one view

I have a uiview with a button on that. When click on that button i am showing a subview.
The subview contains a table view and a search display controller. when i enter something on search bar it will be showing the search result tableview.
once search is done and going back to the superview and again come back to subview, the table view that displays all the records is not scrollable and also the search bar contains the text that has been entered before, that is search bar contains text but the tableview is not search result table view.
This issue will not come if i click on Cancel button of searchbar, without clicking on cancel button if i go back to superview, the issue arises.
When you show yourTableview with searchBar at that time set bellow code..
searching = NO; // if you use this BOOL value..
and also clear the text of UISearchBar's text i.e.
searchBar.text = #"";
and after reloadData of UITableView
-(void)showSearchTable // its an example
{
searching = NO;
searchBar.text = #"";
[yourTableView reloadData];
}

How to entirely disable UITextView's scroll?

I'm developing a simple text editing app for iPad using UITextView. I always had some problems with UIScrollView and UITextView. I think I just expected too much things from these two.
When I set myTextView.text to another a NSString instance, scrolling automatically occurred. I could prevent this scrolling by setting
myTextView.scrollEnabled = NO;
myTextView.text = newText;
myTextView.scrollEnabled = YES;
However, if I changed the selectedRange property of myTextView, scrolling occurred.
Specifically, if selectedRange's range happened on text in the current screen, scrolling didn't occur.
For example, if I select all text by tapping "Select All" property, then scrolling didn't occur. But if I select all text by setting selectedRange to NSMakeRange(0, [myTextView.text length]), then scrolling to the END (the last caret position) occured.
To solve the problems,
1) I saved the original content offset of myTextView.
CGPoint originalOffset = myTextView.contentOffset;
// change myTextView.selectedRange here
myTextView.selectedRange = originalOffset
But nothing happend.
2) I called above codes after a few seconds using NSTimer, and scroll correctly returned to the original position(offset). However, scrolling to the end first occurred and then to the top..
Is there a way to entirely prevent UITextView's scrolling for a moment?
You can disable almost all scrolling by putting the following method into your UITextView subclass:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
// do nothing
}
The reason I say "almost" all scrolling is that even with the above, it still accepts user scrolls. Though you could disable those by setting self.scrollEnabled to NO.
If you want to only disable some scrolls, then make an ivar, lets call it acceptScrolls, to determine whether you want to allow scrolling or not. Then your scrollRectToVisible method can look like this:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
if (self.acceptScrolls)
[super scrollRectToVisible: rect animated: animated];
}

Text shifted up in the textView while i focus on Textview second time

I use Textview for insert message text but when i focus in textview second time after insert some text then already inserted text shifted upside. so text cut in the TextView.
Any solution for this problem?
UITextView the subclass of UIScrollView. So you can discard scroll to top like this:
((UIScrollView*)someTextView).delegate = self; // or some other UIScrollViewDelegate
and implement this method:
-(void) scrollViewDidScroll:(UIScrollView*) scrollView {
scrollView.contentOffset = CGPointMake(0, 0);
}
But if you do need scrolling during edit, you can add some boolean flags to distinguish the situations when user is scrolling and when the scrolling is automatic.

How to pull up a keyboard - iphone app

I've dealt with Keyboards with a UITextField but how can I use a Keyboard in the following way:
I have a UITableView
When the user selects a specific row, a keyboard pops up
I'd like to have a method that knows everytime a letter or something is pressed on the keyboard
Place text fields on the cells using the tableView:cellForRowAtIndexPath: method, and give the text field on each cell its own tag to identify it in the table view or text field delegate methods. Optionally, have a cell's text field become first responder when the cell is tapped in tableView:didSelectRowAtIndexPath: (outside of the text field's region).
To make it look like part of the cells, ensure your text fields don't have borders and match the cell background color.
You can hide a tiny (1x1) invisible/transparent text field in the corner somewhere (temporarily make it a subview in the table view cell or on top of the entire table view), make the text field first responder, and trap any character inputs by implementing the text should change delegate and inspecting the replacement text before it's entered.
const CGRect kbottomPickerViewFrame = {{0.0f, 480.0f},{320.0f, 298.0f}};
const CGRect ktopPickerViewFrame = {{0.0f, 480-298},{320.0,298.0f}};
-(void) animateViewUp:(BOOL)up {
[UIView animateWithDuration:.5 animations:^{
if(up) {
self.view.frame = ktopPickerViewFrame;
}else {
self.view.frame = kbottomPickerViewFrame;
}
} completion:nil];
}
use this method it will animate the view . Call it when textField begin editing