I added a feedback box inside my IOS application, and I want it to take only text to submit the response from the user, but when I tried to enter a white spaces inside the box it took it as a text and accept the submitting! How can I prevent that?
Specify the UIViewController as the delegate to your text view (you can do this either programmatically or specify the delegate in Interface Builder); and
Your UITextViewDelegate method shouldChangeTextInRange needs to check to see if the string to be inserted contains a space:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].location != NSNotFound) {
return NO;
}
return YES;
}
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
In my app i have a comments area, its basically a chat.
The text input is a text view, and whenever the user presses "Return" button, The line breaks.
- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
if ([text isEqualToString:#"\n"])
{
return YES;
}}
Problem is, until the user enters the first letter it looks like so:
I trying to force enter a space (#" ") every time the enter is pressed, and then delete it.. and ether "Hacks" i cam up with, but nothing seems to work..
Any help would be much appreciated
Change #" " with non-breaking space symbol "\u00a0"
- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
if ([text isEqualToString:#"\n"])
{
textField.text = [textField.text stringByAppendingString:#"\n\u00a0"];
return NO;
}
I have a text field where I want to insert a phone number. The problem is that I use number pad and there is not any enter key and I without that key I don't know how to close the text field.
Is there any way of checking how many number os characters there are in a text field and close the number pad when there are X chars?
Sorry for my english and sorry for the noob question but I am just starting with ObjC.
Thanks *
Sorry guys, I forgot to tell you that I already have tried this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length== 9) {
[textField resignFirstResponder];
}
return YES;
}
Yes, you have two choices in my opinion, you can:
A. Check the number of chars returned in delegate method shouldChangeTextInRange and then if your limit is reached resign first responder (when you resign first responder the keyboard dismisses,
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ( textView.text.length + (text.length - range.length) == 10) //or whatever value you like
{
[textField resignFirstResponder];
}
}
Or B:
Override the touchesEnded method of your view and call resignFirstResponder, so that when the user touches the view outside the textField, the keyboard dismisses. - this is what I do in my app.
Like this:
Just add this method to your viewController, it will be called automatically when the touch in the view ends, in this method you simply resignFirstResponder and the keyboard will disappear.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
}
I think it's better to do this in touchesEnded than began, it 'feels' nicer to have the keyboard dismiss when you lift your finger from the view, rather than when to initially touch it.
I found that resignFirstResponder do not include the last char typed. I had to do it in this way,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
DLog(#"Existing: %#, string: %#, range:(%lu, %lu)", textField.text, string, (unsigned long)range.location, (unsigned long)range.length);
if (textField == field2.textField) {
if ( textField.text.length + (string.length - range.length) == kMyNumberLength) // e.g. 10
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
[textField resignFirstResponder];
}
if (textField.text.length + string.length - range.length > kMyNumberLength) {
return NO;
} else {
return YES;
}
}
return YES;
}
Swift
when reaching the maximum number of characters, add the new char to the current text in the textfield and then dismiss the keyboard
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 5
let currentString: NSString = textField.text! as NSString
let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
if newString.length == maxLength {
textField.text = textField.text! + string
textField.resignFirstResponder()
}
return newString.length <= maxLength
}
Every time a user adds a number, the delegate of your text field will be notified through this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger length = [[textField text] length] - range.length + string.length;
if (length == ...) ...
}
You can set up your delegate in such a way as to watch the length of the modified content of your text field, and close the field when the expected length is reached.
However, this has a high potential to frustrate your users a lot: they would make a mistake entering the last character every now and then, and the field is going to close on them, not letting them correct the problem. A better approach is to dismiss the pad when users tap away from your text field, which keeps end-users in control of what is going on:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
}
You can just put a toolbar with a Done on it, on the click on that button you can hide keyboard and toolbar as well.
-(IBAction)doneButtonClicked:(id)sender{
[yourTextField resignFirstResponder];
[toolBar setHidden:YES];
}
or you can use this custom tool
Had issues with some of the suggestions above not retaining the last character entered.
I found this worked well for me.
Method simply examines the length of the text as it is building up and then runs [textField resignFirstResponder] to dismiss.
All usual textField delegates will run for the textField at point field loses focus and keyboard is dismissed.
- (void)textFieldDidChangeSelection:(UITextField *)textField {
NSLog(#"textField.text: %#", textField.text);
if (textField.text.length == 4) // If 4 is your max-length
[textField resignFirstResponder];
}
Use this textfield delegate method to find out the length of textField.text
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int textLength = [textField.text length] + 1;
return YES;
}
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");
}