I've run into a problem with the UITextView that seems to be related to having a scrollable view within a scrollable view.
In order to remedy this i thought i would attempt to write my own multiline (but not scrollable) text view. Given the core graphics methods, and the UITextInputTraits class it seems like this should be feasable. The only thing i cant figure out is wether or not its possible to display (and catch events) for the system wide keyboard.
Is this even possible using the SDK?
What I did in a similar situation, is made a hidden UITextField, and set its delegate to your class where you can implement the appropriate UITextFieldDelegate methods to intercept the key's pressed.
something like this:
UITextField *myHiddenTextField = [[UITextField alloc] initWithFrame: cgRectZero()];
myHiddenTextField.delegate = self;
[myHiddenTextField becomeFirstResponder];
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//use string here for the text input
return false;
}
Related
I am trying to figure out if there is a way to implement an autocomplete functionality in a UITextField for specific values.
I know that the UITextField can do this using the iPhone dictionary (much like searching google in safari, etc), but I want to be able to programmatically have it correct to certain values that I specify.
How to do this?
I did something very similar to this while working on a recent and rather large project. We had a constantly changing list of auto complete terms and built an auto-complete around them.
First, you'll want to make some type of auto-complete controller. It should take a string and return all possible auto complete terms for that string.
-(NSArray *)completionsForString:(NSString *)myString;
Then, check out the UIMenuController class. It's the class that shows the cut/copy/paste options in many applications. You can get the shared instance of it, populate the menu items yourself, and show it above the text field. The user can then simply tap the term they want.
In the end, the solution worked really well for our needs.
Alternatively, you can use this UITextField subclass (inspired by DOAutocompleteTextField):
https://github.com/hoteltonight/HTAutocompleteTextField
It's got a few more features and is actively developed. The example shows you how to use an array as the data source for the autosuggest text. It takes the same approach as DOAutocompleteTextField, in that it shows the suggested completion text "ghosted" in the text field as the user types.
Have you looked into UISearchDisplayController? There are a few threads here on Stack Overflow, including Core Data references if that is what you are using. Also some alternative methods, elsewhere.
With the help of the aforementioned Ray Wenderlich tutorial, I just implemented a version of this to filter names in an existing UITableView.
I set my text field's delegate as my view controller, my view controller as a UITextFieldDelegate and implemented these two methods:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
NSMutableArray *autoCompleteArray = [[NSMutableArray alloc]init];
[self retrieveData];
for(NSString *curString in _staffTableArray)
{
NSString *lowerCaseCur = [curString lowercaseString];
NSRange substringRange = [lowerCaseCur rangeOfString:substring];
if (substringRange.location == 0)
{
[autoCompleteArray addObject:curString];
}
}
if (![substring isEqualToString:#""])
{
_staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
}
[_staffListTableView reloadData];
}
use this delegate method. you can replace values that you specify.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
if ([string isEqualToString:#"StackO"]) {
textField.text=#"StackOverflow";
}
return YES;
}
Just faced with this thread because I need something similar. How about implementing you own search with the UITextfieldDelegate's method:
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
As you'd probably know this method is called for every UITextfield's typing.
I want to set UILabel according to the UITextField as it is typed. I mean if user want to type SAMPLE and he starts typing S then the lable should be set as S, then he types A label should also be A and so on. How to achieve this?
Please share suggestions.
Thanks in advance
Simplest way to do is to make a method and connect it with UiTextfield with event UIControlEventEditingChanged which will give you the trace on every character entered in the textfield.
[self.selectedTextField addTarget:self action:#selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];
-(void)enterInLabel
{
selectedLabel.text=selectedTextField.text;
}
The delegate also works for this as #brain said. The shouldChangeCharactersInRange: method can be a little confusing but the the following I think is pretty straight forward.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// myTextField delegate has been set
if ([textField isEqual:myTextField]){
NSMutableString *txt = [NSMutableString stringWithString:textField.text];
[txt replaceCharactersInRange:range withString:string]; //this is essentially how the textfield is updated after YES is returned
previewLabel.text = txt;
}
return YES;
}
The changing of the text field is actually done after the return YES. That is the whole point of this delegate. Just for an example, if you wanted to limit a textfield to 3 characters you could do the following to stop the text field from "replacing" the text.
if (range.location > 3)
return NO;
Set your textField delegate then call its method in your viewController.m file.. like this -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
mylabel.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
This will change the text after each increment of character.
OR You can add action on your TextField
See this link - UiTextField events
Your view controller will need to implement the UITextFieldDelegate which will allow it to receive changes to the text field. In the appropriate methods of the delegate you need to set the text of the UILabel.
I have never used UITextFieldDelegate so can't provide more detail on how to use it. I would mock up a quick example and just NSLog or debug the delegate calls to see that calls you get.
I have 2 textFields side by side, countryCodeTextField and cellphoneTextField
On countryCodeTextField. I have an action selectCountry that happens on Edit Did Begin on the countryCodeTextField
- (IBAction)selectCountry:(id)sender {
countryCodeTextField.delegate = self;
[countryCodeTextField resignFirstResponder];
Note that self implements the <UITextFieldDelegate>.
Problem is when user click's cellphone the keyboard is displayed if he clicks on countryCodeTextField the keyboard is never dismissed.
If the person clicks the countryCode first then the keyboard never appears(which is what I want).
Why isn't the keyboard hidden when the user clicks cellphoneTextField first and then countryCodeTextField?
If you don't want the user to be able to edit a particular UITextField, set it to not be enabled.
UITextField *textField = ... // Allocated somehow
textfield.enabled = NO
Or just check the enabled checkbox in Interface Builder. Then the textfield will still be there and you'll be able to update it by configuring the text. But as sort of mentioned in comments, users expect UITextFields to be editable.
Also, why are you setting the delegate in the IBAction callback? I would think you'd be better off doing this in Interface Builder or when you create the UITextField in code.
EDIT:
Ok - so you want users to be able to select the box, but then bring up a custom subview(s) from which they select something which will fill the box.
So set the UITextField delegate when you create it (as mentioned above) and implement the following from the UITextFieldDelegate protocol:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
to return NO. Note that if you are using the same delegate for both of your UITextFields, you will need to make this method return YES for the other field. For example, something like:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == countryTextField)
return NO;
return YES;
}
Hopefully this should stop the keyboard being displayed - and now you have to work out how to fire your own subviews, which I'd suggest doing via an IBAction (touch up or something perhaps). You'll have to test various things out here, but remember you're kinda corrupting the point of UITextField and maybe it'll work and maybe it won't, and maybe it'll break in the next iOS upgrade.
Okay, so first, I think you shouldn't be using a UITextField. I think you should be using a UIButton and have the current value showing as the button's title. However, if you have your heart set on it, I would use our good friend inputView, a property on UITextField, and set that to your custom input view (which I assume is a UIPickerView or similar.)
This has the added bonus of not breaking your app horribly for blind and visually impaired users, something you should probably be aware of before you go messing about with standard behaviour.
In your method :
- (IBAction)textFieldDidBeginEditing: (UITextField *)textField
call this :
[textField becomeFirstResponder];
and apply checks for the two fields i.e., when textField is the countryCodeTextField write :
[textField resignFirstResponder];
and call your method :
[self selectCountry];
In this method display the list of country codes.
So Your code will be :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
- (IBAction)textFieldDidBeginEditing: (UITextField *)textField{
[textField becomeFirstResponder];
if (textField == countryCodeTextField){
[textField resignFirstResponder];
[self selectCountry];
}
}
-(IBAction)selectCountry{
//display the list no need to do anything with the textfield.Only set text of TextField as the selected countrycode.
}
I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.
Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
So that could solve your problem.
Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}
Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.
you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...
You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.
I have created a Window based application with the Tab-bar as the RootViewController. On one of the tabs, I've provided a TextBox and TextView. I want to limit the number of characters that can be written inside them (i.e. TextBox and TextView).
Please help if someone knows how to do it..
Thanks..
what do you mean by "tab bar" and "tab"?
i assume you have a UITabBarController as RootViewController and a some subclass of UIView is one of the tabs that is shown when tapping on a TabBarItem.
furthermore i assume your UITextView and (UITextField?) is added to that UIView subclass and NOT the TabBarItem?
if that's the case, you could do a check using the text property:
if([yourTextView.text length] > 42) {
// truncate text; e.g. throw away last part or first part until the length is below 42
}
should be the same for a UITextField - just use the text property again
You should implement the UITextFieldDelegate and/or UITextViewDelegate methods,
textField:shouldChangeCharactersInRange:replacementString:
or
textView:shouldChangeTextInRange:replacementText:
respectively.
Set an instance of the class that implements those methods as the delegate for your view. The logic in the method should examine the incoming text and decide what to do based on the length.
I've found another similar way to implement this problem using Delegates:
#define MAX_LENGTH 20
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length >= MAX_LENGTH && range.length == 0)
{
return NO; // return NO to not change text
}
else
{return YES;}
}