Moving cursor at beginning UItextview [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Moving the cursor to the beginning of UITextField
Hello i have a textview with text I want to move cursor position at beginning I have use NSMakeRange but i don't know why its not working. I have written NSMakeRange is different places , hoping that it would run atleast once but didn't work. Here is the code. thx in advance
- (void)viewDidLoad
{
[super viewDidLoad];
apnatxtView.textColor=[UIColor lightGrayColor];
apnatxtView.text=#"Description goes here";
totalLenght=apnatxtView.text.length;
apnatxtView.selectedRange=NSMakeRange(0,0);
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
apnatxtView.selectedRange=NSMakeRange(0,0);
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[apnatxtView resignFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
if (apnatxtView.text.length == 0) {
apnatxtView.textColor= [UIColor lightGrayColor];
apnatxtView.text=#"Description goes here";
apnatxtView.selectedRange=NSMakeRange(0, 0);
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (apnatxtView.textColor == [UIColor lightGrayColor]) {
apnatxtView.textColor=[UIColor blackColor];
// apnatxtView.text=#"Description goes here";
apnatxtView.text=nil;
return YES;
}
}

This works in my testing on the iOS 6.0 simulator:
- (void)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
textView.selectedRange = NSMakeRange(0, 0);
});
}
I guess it updates the selection based on the touch location after it sends the textViewDidBeginEditing: message. The dispatch_async works around that.

K i found the solution. Im setting the cursor position before cursor appears I cut and paste the code in KeyBoardDidShow notification and it worked pretty fine.

Related

UITextView resignFirstResponder not working

I'm trying to hide the keyboard after the user clicked on the return button of the keyboard.
I'm using this function to hide it:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
When the textview is empty this function works but once there is a characters in the textview,nothing happens and the keyboard doesnt get hidden.
I would suggest to use [self endEditing:YES]; or self.view endEditing:YES]
Found the answer. Was my fault. I had a function which once the user finished editing i'm checking if it's empty or not. For some reason returned no instead of yes. Thanks for the help

UITextView not populated with default text when app has been updated

The premise of my app is simple. The user enters in some information into textFields and textViews and when saved, the TableView is populated with that information (with a combination of Core Data and NSFetchedResultsController).
In version 1 of my app, the "add entry" screen has only UITextFields and no UITextViews. In version 1.1 of my App, I have brought a UITextView for notes. The default text is "Additional Notes: " and this is set in the Storyboard.
Problem
What I'm seeing is, if I create an entry in version 1 and then update my app to version 1.1 from Xcode, the existing entries, when you click on them in the UITableView do not display the word "Additional Notes :" in the UITextView until I actually click on the UITextView cell in which case, my shouldBegin method gets called and it displays it. That is undesirable though because users who upgrade should see the word "Additional Notes: " in the UITextView for existing entries. Essentially Additional Notes is a "placeholder" so if you don't enter in any notes, you should always see those words.
I've set the text in the UITextView in Storyboard but nothing seems to be working.
I have two different view controllers for creating a new entry and editing an entry in Storyboard (because of some slight adjustments) and if you add a new entry, it shows the word Additional Notes :". If you add a new entry but didn't put in any notes, when you edit it, you'll still see the word Additional Notes:. However, entries added from version 1 of the app do not show the word "Additional Notes: " when it comes to editing the entry and I cannot understand why this is the case.
Here are my two main methods:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if([[textView text] isEqualToString:#"Additional Notes: "]){
[textView setText:#"Additional Notes:\n"];
}
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
if([[[textView text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#""]){
[textView setText:#"Additional Notes: "];
}
return YES;
}
So I know I'm missing something, but why can't users who have upgraded, have existing entries displaying the word Additional Notes: in the UITextView without having to click on it.
Any guidance would really be appreciated.
just replace the txt message text in your textview text name,
-(void)viewWillAppear:(BOOL)animated
{
[txtmessage setText:#"Additional Notes:"];
[txtmessage setTextColor:[UIColor lightGrayColor]];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
if(txtmessage.text.length == 0){
txtmessage.textColor = [UIColor lightGrayColor];
txtmessage.text = #"Additional Notes:";
[txtmessage resignFirstResponder];
}
return NO;
}
if(textView.text.length >=65)
{
[textView resignFirstResponder];
return NO;
}
if([text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound) {
return YES;
}
[textView resignFirstResponder];
return NO;
return YES;
}
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
if (txtmessage.textColor == [UIColor lightGrayColor]) {
txtmessage.text = #"";
txtmessage.textColor = [UIColor blackColor];
}
return YES;
}
-(void) textViewDidChange:(UITextView *)textView
{
if(txtmessage.text.length == 0){
txtmessage.textColor = [UIColor lightGrayColor];
txtmessage.text = #"Additional Notes:";
[txtmessage resignFirstResponder];
}
}

Focus of cursor is not shifting to next UITextView in ios

This is my First question on ios
I am using two UITextView objects (textView1 and textView2) in a View, Each of them has some character limit with some following Scenario:
Initially user can only enter into textView1.
When the entered character limit of textView1 is over, the cursor will automatically shift to textView2.
After building the project, If user tap the textView2 and try to write into it, Cursor must shifted to textView1 (because it is empty).
I wrote the code and everything works fine except the third scenario, User can only enter into textView1 but focus is still on textView2
Steps to reproduce:
Build the project
user tap the textView2 first and try to write something.
According to written code, Focus remain in textView2 but user are writing into textView1 (see the attachment)
Here is the snapshot:
Here is the written code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.textView1 becomeFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSInteger restrictedLengthForTextView2 = 130;
NSString *temp=textView.text;
if(textView == self.textView1){
if([[textView text] length] > restrictedLengthForTextView1){
textView.text=[temp substringToIndex:[temp length]-1];
[textView resignFirstResponder];
[self.textView2 becomeFirstResponder];
}
}else{
if([[textView text] length] > restrictedLengthForTextView2){
textView.text=[temp substringToIndex:[temp length]-1];
[self.textView2 resignFirstResponder];
}
}}
- void()textViewDidBeginEditing:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSLog(#"dalknwdlakwd");
if([[self.textView1 text] length] < restrictedLengthForTextView1){
if(textView == self.textView2){
[self.textView2 resignFirstResponder];
[self.textView1 becomeFirstResponder];
}
}}
Please help me here..
please do as per following:
in .h file
#interface ViewController : UIViewController<UITextViewDelegate>
{
IBOutlet UITextView *txtView1;
IBOutlet UITextView *txtView2;
}
#end
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[txtView1 becomeFirstResponder];
txtView2.editable=NO;
}
implement the textView delegate method like below:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(textView.tag==1)
{
if([textView.text length]>25)
{
txtView2.editable=YES;
[txtView2 becomeFirstResponder];
}
}
return YES;
}
i have taken text length limit in first textview as 24 characters as an example.
i hope this will help you.
It is a known bug, with resigning and becoming first responder within the same runloop. Try the following
[textView2 resignFirstResponder];
[textView1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
An update to Annadurai's answer: it's still a bug in 2016! After trying others' suggestions to deselect the surrounding tableview cell, and/or change the tint color only this answer worked for me:
[textView1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
FYI My case was that I was resigning a textView, reloading the table, and returning to the same textView. Before this fix - no cursor on the reloaded textView (Yes I could type and see characters.) After the performSelector... afterDelay - the cursor was visible again.

Prevent editing of text in UITextField and hide cursor/caret/magnifying glass while using an inputView

How do I prevent the editing of text in a UITextField along with hiding the cursor/caret/magnifying glass, but still present the keyboard, as I am using an inputView with a UIPickerView/UIDatePickerView?
Setting userInteractionEnabled to NO does not work, because it no longer receives any touches and will not present the keyboard.
Subclass UITextField
//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}
//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
gestureRecognizer.enabled = NO;
}
[super addGestureRecognizer:gestureRecognizer];
}
In your UITextFieldDelegate
//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
Now just set your text programmatically from the result of your UIPickerView/UIDatePicker.
Hiding the cursor is a lot simpler in iOS 7. Still need some trickery to disable the loupe
textField.tintColor = [UIColor clearColor];
I hope this will helpful for you.
Set cursor UIColor -> Empty. In UI, it will be hidden.
[[self.textField valueForKey:#"textInputTraits"] setValue:[UIColor clearColor] forKey:#"insertionPointColor"];
I found the best solution was
- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:) || action == #selector(selectAll:) || action == #selector(paste:))
{
returnNO;
}
return [super canPerformAction:action withSender:sender];
}
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
To have a UITextField without interaction, but still work with an inputView:
Subclass UITextField with these methods:
// Hide the cursor
- (CGRect)caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
// All touches inside will be ignored
// and intercepted by the superview
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return NO;
}
The last method will single-handedly prevent any editing and the magnifier glass, as you won't be able to tap the UITextField.
This works great if you're using a textfield in a UITableViewCell for example, and then can toggle firstResponder status through tableView:didSelectRowAtIndexPath:.
To disable any interaction with textfield except for making it a first responder you can just place a UIButton of the same size right over the textfield. Code for button tap event could be something like this:
- (IBAction)btnEditPhoneTapped:(id)sender
{
if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
}

ipad: how to know editing done in UITextView?

I've a textview. I want to know whether the changes done in textview while pressing back button in UINavigationBackbutton. how to compare old and new text entered in UItextview?
If there is any changes, i'll ask Do u want to save the changes?.
Just set a flag when your UITextView did begin editing (wasEdited=YES) and save the current state of the text (originalText = myTextView.text) then on backbutton check the (originalText isEqualToString:myTextView.text && wasEdited)
The was edited tag is to avoid string comparaison in case the user didn't get into editing :)
iphony,
Better late than never...
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(#"textViewShouldBeginEditing");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"textViewDidBeginEditing");
}
- (void)textViewDidChange:(UITextView *)textView {
NSLog(#"textViewDidChange");
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSLog(#"textViewDidChangeSelection");
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(#"textViewShouldEndEditing");
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
NSLog(#"textViewDidEndEditing");
}

Resources