Restricting copy paste of string in numeric textfield - iphone

I have a textbox in which I wanted a user to enter only numbers. I have implemented the number keypad there. But if someone puts my app in the background and copies some character string from some other app and comes back to my app and pastes it, it successfully pastes the string content into my numeric textfield. How can I restrict this scenario?

#theChrisKent is close, but there's a slightly better way. Use the delegate method -textView:shouldChangeTextInRange:replacementText:. Check if replacementText contains any non-numbers, and if so return NO.

You can disable pasting altogether by following the top answer on this question: How disable Copy, Cut, Select, Select All in UITextView
Just subclass the UITextView and override this method (code stolen from above question):
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(paste:)
return NO;
return [super canPerformAction:action withSender:sender];
}
Otherwise you can implement the UITextViewDelegate protocol and implement the textViewDidChange: method and check if it's numeric. If not, undo the changes. Documentation here: http://developer.apple.com/library/ios/documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intfm/UITextViewDelegate/textViewDidChange:

Related

Updating an NSTextField while controlling an NSTableView [duplicate]

What's the easiest way to have an NSTextField with a "recommendation list" dynamically shown below it as the user types? Just like Safari's address bar that has a menu of some sorts (I'm pretty confident Safari's address bar suggestions is menu since it has rounded corners, blue gradient selection, and background blurring).
I've tried using NSTextView's autocompletion facility but found it was inadequate:
It tries to complete words instead of the whole text fields – in other words, selecting an autocomplete suggestion will only replace the current word.
It nudges the autocompletion list forward and align it with the insertion point instead of keeping it align with the text field.
In the sample screenshot above whenever I selected the autocomplete suggestion the text field only replaces K with the suggested item in the list, which results in Abadi Abadi Kurniawan.
These are what I'd like to achieve:
Whenever a suggestion is selected, the entire text field is replaced with the suggestion.
Keep the suggestion list aligned with the text field's left side.
Note: This is not a question about adding progress indicator behind a text field.
The Safari address bar uses a separate window. Apple has example project CustomMenus and it only takes an hour or two to customize it.
Developer session explaining what has to be done Key Event Handling in Cocoa Applications
If you want to be able to select multiple words you need to provide own FieldEditor (credits should go for someone else)
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(nullable id)client;
{
if ([client isKindOfClass:[NSSearchField class]])
{
if (!_mlFieldEditor)
{
_mlFieldEditor = [[MLFieldEditor alloc] init];
[_mlFieldEditor setFieldEditor:YES];
}
return _mlFieldEditor;
}
return nil;
}
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
// suppress completion if user types a space
if (movement == NSRightTextMovement) return;
// show full replacements
if (charRange.location != 0) {
charRange.length += charRange.location;
charRange.location = 0;
}
[super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];
if (movement == NSReturnTextMovement)
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"MLSearchFieldAutocompleted" object:self userInfo:nil];
}
}
This only addresses half of your answer, but I believe you need to subclass NSTextView and implement the - (NSRange)rangeForUserCompletion method, returning the range of the entire string in the text field. This should make sure that it doesn't just autocomplete the most recently entered word.
If you want a custom menu, you're going to have to do that yourself, probably by implementing the -controlTextDidChange: method and displaying a custom view with a table when appropriate.

paste text by code [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Copy and paste text with buttons
I want to make past from the clipboard by code with out toggling the screen in iPhone , Any one have any idea ?
I want to make these operation by code not like these.
Um asking if these is possible or um accessing the iOS in that case ?
[UIPasteboard generalPasteboard] provides you an instance of UIPasteboard which repents the users current clipboard content. Use it like
- (IBAction)pasteButtonPressed {
self.myTextView.text = [[UIPasteboard generalPasteboard] string];
}
The UIResponderStandardEditActions informal protocol declares the paste: method that is designed for that purpose. UITextView (along with other UIResponder subclasses) conforms to this informal protocol and thus responds to this method.
This has the advantage of managing every aspect of the pasting operation, especially pasting the text at the position of the insertion point, or replacing the selected text if any, as with a classic paste operation.
Another solution is to insert the text contained in the UIPasteboard yourself in your text view. But be sure in that case to replace the selectedTextRange (selected text or insertion point) and not replace the whole content of the UITextView.
For this purpose, use the fact that UITextView implements the UITextInput formal protocol, which declares every method needed to handle objects that manage text input (like UITextView, UITextField, etc) especially replaceRange:withText: and selectedTextRange:
NSString* pasteboardText = [[UIPasteboard generalPasteboard] string];
[self.myTextView replaceRange:self.myTextView.selectedTextRange withText:pasteboardText];

UITextView not firing textViewDidChange on attribute change?

I have a UITextView configured with a delegate. I've enabled editing attributes by setting allowsEditingTextAttributes to YES.
When the user types a character in the text view, the delegate receives the textViewDidChange: message.
But when the user changes an attribute (such as making a selection and tapping Bold or Italic), no textViewDidChange: message.
The documentation specifically says that textViewDidChange: should receive a message when the user changes attributes:
Tells the delegate that the text or attributes in the specified text view were changed by the user.
But it's not working for me. What am I missing here?
I tested this scenario in iOS 6 and had the exact same outcome: attribute changes from the "select" pop-up did not trigger the textViewDidChange: method. It seems that this is a bug or the documentation needs to clarify what type of attribute change would trigger this event.
A possible workaround is to implement the textViewDidChangeSelection: method. It gets called whenever a selection is made (which the user would have to do before changing an attribute). Check to see if the selectedRange.length is > 0 (which would mean an actual word has been selected, instead of just moving the cursor around), and save that selectedRange. Once the length is zero again, it means they deselected the item. At that time, you could take the previous range and work with the text.
- (void)textViewDidChangeSelection:(UITextView *)textView
{
static BOOL rangeSet = NO;
static NSRange mySelectedRange;
if( textView.selectedRange.length > 0 && !rangeSet )
{
mySelectedRange = textView.selectedRange;
rangeSet = YES;
}
else if( textView.selectedRange.length == 0 && rangeSet)
{
// Work with text
NSLog(#"Working with previously select text: %d, %d", mySelectedRange.location, mySelectedRange.length);
}
}
In documentation it has also been mentioned that
This method is not called in response to programmatically initiated changes
So if you are setting bold or italic programatically so it will not invoke this delegate method
Please check if you've set UITextViewDelegate in in your .h file. Also you'll have to `make
yourTextView.delegate = self
So that your Text View will give control to your current class.
I got around this by subclassing UITextView, overriding the toggleBoldface:, toggleItalics: and toggleUnderline: methods, and calling [self.delegate textViewDidChange:self] from those methods. This answer helped me figure that out.

Detecting whether the user has typed in a UITextField

I know that if you use the following line of code, you can detect what the user has typed in a TEXT VIEW.
if ([[textView text] isEqualToString:#"")
I want to detect whether the user has typed anything in a text FIELD, and this does not work with text fields. What should I change? Thanks for your help!
The UITextFieldDelegate is the preferred way to find this out.
- (void)textFieldDidBeginEditing:(UITextField *)textField - this will only tell you that it has become the first responder / key field, this does not guarantee that the user modified the value
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - this method will tell you when ever the user hits a key to change the text, a "paste" will cause multiple characters to be in the string, backspace over one, or delete of a selection will cause the string to be empty, …
- (void)textFieldDidEndEditing:(UITextField *)textField - this is a common location to check the value in the field against the value in the model to see if an edit has occurred. this will only be called when the field relinquishes "key" status, note that a user could tap a button to cause the whole view to go away before this is called, often making it useful to track if any field you are interested in becomes the "key field" (see first method)
it can be useful to set break points in relevant implementations to familiarize yourself with their call order/logic
You can retrieve contents of a UITextField the same way you can a UITextView, by accessing the "text" property. The code below is if a text field is not empty.
if (![someTextField.text isEqualToString:#""])
{
NSLog(#"someTextField is not empty");
}
You can also use the UITextField delegate to detect when a user starts typing, stops typing, etc. More information about that is available in the documentation.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/doc/uid/TP40006991
Finally, here's a link to the UITextField docs.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/doc/uid/TP40006888
you can check for text length instead of content
if ([[textView text] length]==0)

Compare multiple strings in textview of iphone

I want to compare all the strings entered in a textview of iphone. Once the user finishes with typing in textview I want to compare strings entered by the user.
How do I do it?
Please help
I am not sure what you define "finishes with typing", here is my guess:
You have to set up your class become UITextViewDelegate and implements some methods:
If you want to get the text whenever the text changes, use:
- (void)textViewDidChange:(UITextView *)textView
If you want to get the text whenever the text view resign first responder, use:
- (void)textViewDidEndEditing:(UITextView *)textView
I also don't know what you define by comparing all the NSString. One way to do is put them into NSSet and NSSet itself will remove duplicates of NSString for you