I am making an app with multiple textfields. I would like when the user enters a number in the textfield, to automatically add the percent symbol when editing ends. Right now, the only way i know how to do that is to convert the text value to float, then use NSNumberFormatterPercentStyle and then send back the value. I guess that there should be a simpler and faster way to do that for multiple textfileds. Does anyone know?
You could use the UITextFieldDelegate's textFieldDidEndEditing: method.
For example:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *oldTextFieldValue = textField.text;
textField.text = [NSString stringWithFormat:#"%# %%",oldTextFieldValue];
}
Note the double "%" in the stringWithFormat: method: this is because a single % on its own is a "format specifier" and will not be taken into account and xcode will give you a warning, so you need to protect it with a second %.
Related
I am trying to implement a textView in which when user starts typing(Let's say names) it would show up the suggestions and when they click them it gets added to the textView than user presses comma and again the same functionality for another name....
And at the end the text in the textView should look like this...
Aron,Maria,Alex,Cassie
Can any one suggest me How can I achieve this?
(Its somewhat similar to adding the "Tags" while posting this question!!!)
Thanks.
You can use a NSTokenField replacement there is some libraries here :
tokenField libraries
Following link may help you:
http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values
Follow the same flow. To get autocomplete suggestions after comma modify the delegate method as found below.
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *names = [NSString stringWithString:textField.text];
NSArray* arr = [names componentsSeparatedByString:#","];
NSString *subString = [arr lastObject];
substring = [substring
stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
Provide a NSMutableArray named 'allNames' which contains all the names you want to display in the suggestion list and use it as the following:
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[autocompleteUrls removeAllObjects];
for(NSString *curString in allNames) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
When the user clicks the suggestions display the name by appending with previously entered names.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// set the textField.text by appending this name to already entered names
}
I didn't see anything like this in mobile app. You can look for libraries. But if you want to make it yourself I would advice you to use invisible tableView. When user starts typing name you should fetchData and show in tableView under textView. It's not hard.
A relatively easy way I can think of to achieve this functionality would be to add an input accessory view to your keyboard, which will offer suggestions.
You wouldn't have to tamper with the TextField itself, nor would you need to incorporate the suggestions into the remainder of your apps layout.
The accessory view could, for example, be given a reference to the textfield and listen to input by:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textChanged) name:UITextFieldTextDidChangeNotification object:textFieldWithSuggestions];
It would feature a method -(void)textChanged; in which you would have the opportunity to split the existing text into components, using comma or whatever symbol as a separator, and then use the last text fragment to perform your search for possible completions.
It may present these suggestions as a row of buttons for example (maybe even in a side-scrolling scrollview to allow for many suggestions) and if one gets pushed, update the textfields text by replacing the last text segment with the completed string.
To keep track of which button stands for which suggestion, just give them tags according to the indices of your search results. This way, you'll need only one method as a target for the buttons, too.
If you want to some library code then you can go for this
https://github.com/hoteltonight/HTAutocompleteTextField which will help you
I'm creating a text field that allows the user to enter a date, and I want to show the format required: mm-dd-yyyy. As they type, it should replace the format with the numbers they type. For instance, if they enter 125, it should look like: 12-5d-yyyy.
I've been able to get this working (using the textField:shouldChangeCharactersInRange:replacementString: method). But there are 2 problems:
When I update the text field so that it shows the format plus what they have typed (by directly setting textField.text) the cursor goes to the end of the inserted text. For example, it currently looks like: 12-30-yyyy| (where | is the cursor), but I want it to look like 12-30-|yyyy. So how can I place the cursor where they last typed?
I have not been able to determine where a deletion occurs if the user presses backspace or delete. The only way I know to determine that they pressed backspace or delete is like this: BOOL thisIsBackspace = ([string length] == 0) (where string is the value of replacementString:. But this doesn't tell me where it occurred. So how can I determine where a deletion occurs in UITextField?
Using the UIDatePicker would be the way to go. Aside from that...
In textField:shouldChangeCharactersInRange: the range parameter will tell you where where in the field the actual change is occurring.
You can also use stringByReplacingCharactersInRange to create the value of the field after edit. Then you can use it to compare and find where they edited.
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Now you can compare text and textField.text and find where they are different.
return YES;
}
You can place the cursor by using:
[textField setSelectedRange:NSMakeRange(desiredPosition, 0)];
You can determine where the deletion was made by using the "range" input for the method
textField:shouldChangeCharactersInRange:replacementString:
On my app I need to do this: when a character is typed on TextView to be saved on a NSString and after that to be replace with '*'. I tried this :
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(#"typing...");
text=#"*";
passwordText=textView.text;
NSLog(#"password %#",passwordText);
NSString* nextText = [textView.text stringByReplacingCharactersInRange:range withString:text];
textView.text=nextText;
NSLog(#"next %#",nextText);
NSLog(#"textview.text %#",textView.text);
return YES;
}
where passwordText is the NSString in which I want to save the text introduce from keyboard on UITextView.
The result is this : http://i54.tinypic.com/2cx9ueo.png (here I introduced 'we' and I see this :'*w*e'. Can anyone help me to solve this?
I mention that I must do this using UITextView, and not UITextField.
I can tell you why you get character along with the *, though i am not sure whether your approach is worth to go through this.
make your return statement as NO, this will discard the new key pressed. The YES is currently placing that character next to your programmatic '*'.
Just return a NO in the method if you want the change to be immediate. If you want it to be a little delayed (i.e. first show a character then replace with * like in password fields), return a YES and run another method from the
textView:shouldChangeTextInRange:replacementText: method to be fired after 0.5 seconds (or another number if you like) using a timer.
This new method can replace the last added character or changed character with a *.
I'm putting up a CalculatorApp for the iPhone and there is just one thing that i need to end, the floating-point!
As in normal calculators, i need to do something so that will only permit one "." .
Can you dudes help me?
you have a few ways to go, such as, NSString's rangeOfString method, e.g.
#define FLOATING_POINT_STRING #"."; // set this to #"." or #"," according to the floating point type you want to use
float calculatorText = 45.194; // set this to whatever the label says, or you can skip the float => string conversion as shown below
NSString *text = [NSString stringWithFormat:#"%f", calculatorText];
if ([text rangeOfString:FLOATING_POINT_STRING].location != NSNotFound)
{
// do nothing, there is a floating point
}
else
{
// append FLOATING_POINT_STRING to the label
}
Good luck ;)
Are you using a UIButton for the decimal point button? If so, you could simply disable it as soon as it is pressed. And then of course, re-enable it when "clear" or "equals" or whatever is pressed:
[buttonDecimalPoint setEnabled: NO];
I have a UITextView and 2 UITextField set up. UITextView resigns first responder status when empty part of the screen is tapped, the same for the 2 UITextField, plus for these 2, the return key also resigns first responder status. All 3 are declared in interface.
I would like to get the contents of all of these to individual NSString and/or learn how to enter them directly into something like:
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://server.com/file.php?var1=%#&var2=%#&var3=%#", *content of UITextView*, *content of UITextField*, *content of UITextField*];
This is a very basic question, i know, but i'm pretty much a novice. If i learn how to do this i'll probably be able to pick up from there.
cheers
(edited)
UITextField and UITextView both have a text property that you can use to retrieve the string values. For example,
NSString *string = [NSString stringWithFormat:#"%#, %#", textField.text, textView.text];
Keep in mind you'll probably want to examine the strings to make sure they're not empty or contain invalid characters before putting them into a URL.
The accepted answer is good, I just wanted to add the following for an expanded look at grabbing text in iOS.
See the textInRange: aspect of the below code that I devised to use one function to determine the text whether it's a UITextField, UITextView or any other class that complies with the UITextInput protocol.
//handle text container object length whether it's a UITextField, UITextView et al
NSUInteger LengthOfStringInTextInput(NSObject<UITextInput> *textContainer)
{
UITextPosition *beginningOfDocument = [textContainer beginningOfDocument];
UITextPosition *endOfDocument = [textContainer endOfDocument];
UITextRange *fullTextRange = [textContainer textRangeFromPosition:beginningOfDocument
toPosition:endOfDocument];
return [textContainer textInRange:fullTextRange].length;
}
By changing the return type to NSString and removing .length you could have the functionality of the text property on any class.