iOS Programming:Correcting Text - ios

So I've been developing an iOS application, and one part of it involves the user entering a paragraph of text, and I need my app to filter the text, and use Apple's autocorrect function, to rectify mistakes in the text. For example, if the text is-
The quick brown fox jumpet over the lazy dog
Then it should be able to take the word 'jumpet' and change it to jumped. Does anyone know how this can be done? And I don't have to prompt the user, I'm planning to run this code in a background thread, while an activity indicator spins.
Thanks A Lot!
Raghav
P.S. - The text is in an NSString...

You can manually check spelling in a string using UITextChecker.
I haven't used it myself but it looks pretty straightforward.

Seems Apple doesn't give a public API for autocorrections,
So you should do this with some tricks and hacks.
This article should be useful.
http://blog.persistent.info/2013/10/programmatically-accepting-keyboard.html

Copied From here
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// Turn spell check on
textView.autocorrectionType = UITextAutocorrectionTypeYes;
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
// Turn spell check off and clean up red squiggles.
textView.autocorrectionType = UITextAutocorrectionTypeNo;
NSString *currentText = textView.text;
textView.text = #"";
textView.text = currentText;
return YES;
}

Related

Configuring keyboard settings within PDFView form fields

I'm working on an app which loads PDF files from a server and displays those PDF files within a PDFView. The files contain form fields in which the user is to type. That's works fine. The PDF files are to be used in an education setting, where the spelling should not be autocorrected and predictive text should not be available.
I have not found the means to disable autocorrect in a PDFView, along the lines of autocorrectionType = false in a UITextField.
I'm aware the user can manually disable autocorrection and predictive text in device settings. That's not a viable option in this case (likely user confusion and no means to verify). I'm ok if there's a way to disable autocorrect app-wide.
We're creating the PDF files in-house, so we're ok if there's something we can do while generating the files. Adobe Acrobat is a "check spelling" option on form fields, but the setting has no effect, at least within PDFView.
Thanks.
I found a solution. It's a hack, but I'll take what I can get.
I found that when a user taps a PDF text field, PDFKit hides the PDF text field and overlays a UITextView at the same location. It makes that UITextView the first responder and brings up the keyboard. That UITextView remains until the user taps elsewhere, when it is removed and replaced with a PDF text field containing the contents of the (now dead) UITextView.
The UITextView in question is buried deep inside PDFView, within private UIView subclasses.
Below is the code I'm using. It starts with a view (the PDFView) and deep-dives looking for any UITextView it can find. When found, it resigns as first responder, changes parameters, and becomes the first responder again. The user will see the typeahead buttons appear briefly then disappear. I haven't found a way around this, as we don't gain access to the UITextView until it is already the first responder.
The code here is called via a timer executing every 0.1 seconds. I'm sure there are more efficient ways to do this but this works, and barely registers on the CPU meter.
This code also sets the pasteDelegate of the UITextView because in my case I want to override and prevent pasting of text into the UITextView. The code to do that is simple; in textPasteConfigurationSupporting just return [item setNoResult].
As with all hacks like this, be sure to test with all versions of iOS your app supports - including future versions. Apple could easily change their PDFKit implementation causing this to break or misbehave. Or better, they could add a supported means to do this.
-(void)lookForTextViewsInView:(UIView *)view
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)subview;
//NSLog(#"Found text field with contents: %#",textView.text);
if (textView.autocapitalizationType == UITextAutocapitalizationTypeNone &&
textView.autocorrectionType == UITextAutocorrectionTypeNo &&
textView.spellCheckingType == UITextSpellCheckingTypeNo &&
textView.pasteDelegate == self) {
//NSLog(#"textView %# is already adjusted", textView.text);
return;
}
if (textView.isFirstResponder) {
//NSLog(#"Adjusting and resetting first responder of %#",textView.text);
[textView resignFirstResponder];
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
textView.autocorrectionType = UITextAutocorrectionTypeNo;
textView.spellCheckingType = UITextSpellCheckingTypeNo;
textView.pasteDelegate = self;
[textView becomeFirstResponder];
} else {
//I don't think this ever fires, but here for completion's sake
//NSLog(#"Adjusting without resetting first responder of %#",textView.text);
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
textView.autocorrectionType = UITextAutocorrectionTypeNo;
textView.spellCheckingType = UITextSpellCheckingTypeNo;
textView.pasteDelegate = self;
}
} else {
//NSLog(#"%# is not a UITextView", [subview class]);
[self lookForTextViewsInView:subview];
}
}
}

Share image in iMessage using Custom Keyboard in iOS 8

I am working on iOS 8 custom keyboard, where i have designed keyboard using some images like smiley. i want this keyboard to be work with iMessage. when i am trying to send text its working properly but can't able to share image there. I have tried following code :
To share text : (its working properly)
-(void)shouldAddCharector:(NSString*)Charector{
if ([Charector isEqualToString:#"Clear"]) {
[self.textDocumentProxy deleteBackward];
} else if([Charector isEqualToString:#"Dismiss"]){
[self dismissKeyboard];
} else {
[self.textDocumentProxy insertText:Charector];
}
}
To add image : ( Not working)
-(void)shouldAddImage:(UIImage*)oneImage
{
UIImage* onions = [UIImage imageNamed:#"0.png"];
NSMutableAttributedString *mas;
NSTextAttachment* onionatt = [NSTextAttachment new];
onionatt.image = onions;
onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height);
NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];
NSRange r = [[mas string] rangeOfString:#"Onions"];
[mas insertAttributedString:onionattchar atIndex:(r.location + r.length)];
NSString *string =[NSString stringWithFormat:#"%#",mas];
[self.textDocumentProxy insertText:string];
}
Is there any possibility to pass image to [self.textDocumentProxy insertText:string];
following attached image shows how exactly i want to use this image keyboard. i am surprised how emoji keyboard will work?
As far as I know, the behavior you are looking for is not possible as of iOS 8 beta 4.
Currently, the only way for iOS custom keyboards to interact with text is through <UITextDocumentProxy> and the only way to insert anything is via the insertText: method.
Here is the header for the insertText: method in <UITextDocumentProxy>:
- (void)insertText:(NSString *)text;
As you can see, it takes a plain NSString... not an NSAttributedString. This is why your attempt to insert an image doesn't work.
However, despite the fact that you can't add pictures, it is still very possible to insert emojis, since an emoticon is really just a Unicode character.
To add an emoji, just insert the proper Unicode character:
[self.textDocumentProxy insertText:#"\U0001F603"];
Useful links:
List of Unicode Emoji: http://apps.timwhitlock.info/emoji/tables/unicode
Unicode Characters as NSStrings: Writing a unicode character with NSString

UITextField input and image

I am totally new to programming and Objective C so I am sorry if I sound unclear. I am trying to create an app where the user answers a quiz with input in a text field. When the answer is correct an image for correct answer should appear. It sounds so easy but I do not know how to achieve this and so far I was not able to find tutorial or exact answer to such question. Any help is welcome and I won't give up this early. Thanks.
Try out this tutorial:
http://www.raywenderlich.com/1845/ios-tutorial-how-to-create-a-simple-iphone-app-tutorial-part-2
It shows you how to create a simple app with a text field and image. I'm sure you can piece everything else together.
After user entering text check with the answer if its correct show imageview else show error view
-(IBAction)checkAnswer:(id)sender
{
if([self.answerTextfield.text isEqualtoString:#"xxxxxx"])
{
self.imageView.image = [UIImage imageNamed:#"correct.png"];
[self.view addsubView:self.imageView];
}
else
{
//show error image
}
}
Link has a tutorial on using TextField
http://www.tutorialspoint.com/ios/ios_ui_elements_text_field.htm
customize it according to your needs..
Image can be shown using UIImageView - for this study how to use UIImageView
Hope it helps you to move forward.Try hard
Use this delegate method in "#Natara" 's answer.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([self.answerTextfield.text isEqualtoString:#"xxxxxx"])
{
self.imageView.image = [UIImage imageNamed:#"correct.png"];
}
else
{
//show error image
}
return NO;
}
This delegate method will be called each time any character is added or deleted. So this mehtod is better option instead of creating a method like "#Yohan" did.
Hope this helps. Cheers :)

Size of bullets in UITextField with secureTextEntry changes size as focus switches when using custom font

I have a UITextField that is using the Museo Sans Rounded 300 font. Everything works fine for normal UITextFields, but when you set the secureTextEntry = YES, then there's this disconcerting change to the size of the bullets as the UITextField gets and loses focus (i.e. becomes, and relinquishes, being the first responder).
When the UITextField has focus, the bullets appear to be using the custom font, but once it loses focus they change to being these much bigger (standard size) bullets.
So, the only way I found to combat this was to use the textFieldDidBeginEditing and textFieldDidEndEditing delegate methods, keep track of what was entered in the text field, replace it with a mask of bullets, and disable secureTextEntry. So, when they leave the field, they’re actually just seeing the right number of bullets, rather than their secured text. It’s hacky and messy, but it’ll do for me, perhaps for you, too.
I found an easy solution an it works quite good.
Basically you have to change the font to a custom font when you set secureTextEntry to yes.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if([textField.text isEqual:#"Password"]){
textField.text = #"";
textField.font = [UIFont fontWithName:#"Helvetica" size:14.5];
textField.secureTextEntry = YES;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
if([textField.text isEqual:#""]){
textField.text = #"Password";
textField.secureTextEntry = NO;
textField.font = [UIFont fontWithName:#"YourFont" size:14.5];
}
}
Another workaround:
While this is an iOS bug (and new in iOS 7, I should add), I do have another way to work around it that one might find acceptable. The functionality is still slightly degraded but not by much.
Basically, the idea is to set the font to the default font family/style whenever the field has something entered in it; but when nothing is entered, set it to your custom font. (The font size can be left alone, as it's the family/style, not the size, that is buggy.) Trap every change of the field's value and set the font accordingly at that time. Then the faint "hint" text when nothing is entered has the font that you want (custom); but when anything is entered (whether you are editing or not) will use default (Helvetica). Since bullets are bullets, this should look fine.
The one downside is that the characters, as you type before being replaced by bullets, will use default font (Helvetica). That's only for a split second per character though. If that is acceptable, then this solution works.
i just test result above, #Javier Peigneux's answer is the most concise
#pragma mark -- UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UCSSafeTF *)safeTF{
safeTF.font = [UIFont fontWithName:#"Helvetica" size:14];
}
- (void)textFieldDidEndEditing:(UCSSafeTF *)safeTF{
safeTF.font = [UIFont fontWithName:#"Helvetica" size:14];
}
now i write like this, and the result is OK. then the reason why you see the bullets size change from small to big is very clear, just because apple iOS 10 below "help" us resize the custom font. hope will help you .
Just create a method that gets called every time the show/hide password toggle is selected. Inside the method, set the font to nil, then set the font to your custom font and font size. You should be setting the custom font and size in the viewWillAppear method as well. Inside this method, you're re-setting it.
This way, you don't need to disable secureTextEntry(which could make your text field vulnerable) and you don't need to use textFieldDidBeginEditing or textFieldDidEndEditing.
Code Example:
//if the password is obscured and the toggle to show it has been turned on, display password. Else, obscure it.
- (IBAction)togglePasswordVisibility:(id)sender {
// Xcode contains a bug where the font changes to default font if these two lines of code are not included.
self.passwordInputTextField.font = nil;
self.passwordInputTextField.font = [UIFont fontWithName:#"myCustomFontName" size:myDesiredFontSize]; //set this in viewWillAppear as well!
if (self.passwordInputTextField.secureTextEntry == YES) {
self.passwordInputTextField.secureTextEntry = NO;
[self.showHideButton setTitle:#"HIDE" forState:UIControlStateNormal];
} else {
self.passwordInputTextField.secureTextEntry = YES;
[self.showHideButton setTitle:#"SHOW" forState:UIControlStateNormal];
}
}

Add a permanent Character to UITextField

Is there a way to permanently add a letter to a UITextField where the user cannot delete it? I want to add a single character and the user not be able to delete it but they can still add letters after.
Cheers,
p.s. This is for iOS
A UITextField has a delegate method called should change characters in range, this method basically ask, should i add or remove the next character? and from that you can dictate what you would like. Here is some example code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL shouldChangeCharacters = ( (textField.text.length + string.length) > 1 );
return shouldChangeCharacters;
}
This code means if the new character being added plus the current text is greater than 1 then it is okay to make the change, if the text is not greater than one, then we will not make the change...
Now, under the assumption that someone may try to paste over your character, this delegate method is still called but you have to do a few things.
if (range.location == 0) {
NSString* firstCharacter = [string substringToIndex:1];
BOOL firstCharacterIsDesiredCharacter = [firstCharacter isEqualToString:#"#"];
if ( !firstCharacterIsDesiredCharacter ) {
NSString* symbolWithText = [NSString stringWithFormat:#"#%#",text];
// ******************** \\
[textView setText:symbolWithText];
return NO;
// or we could do this
string = symbolWithText;
// ******************** \\
}
}
Both scenarios, are modifying the values of the parameters... some people don't like doing that. It could be a bad programming practice.. or if you are going to modify them there's some process you should do first.
With that, we only need to run this code if they are trying to replace the first character, i substituted the hash tag symbol, the first character is from a range of location 0 and length of 1. So if the range is equal to 0, then we run our code to fix it. This code also takes into consideration that they might be pasting the special symbol with it. so if the UITextField read #work, and they tried to copy "work" or "#work" it takes both scenarios into consideration and completely skips the code if the hash mark is the first character.
UITextField Reference
try this
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = #"left";
label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = label;
Try using this:
in your .h:
IBOutlet UITextFeild *textFeild;
in your .m:
- (BOOL)textFieldShouldReturn:(UITextField *)textFeild {
NSString *textFeildText = textFeild.text;
textFeild.text = [NSString stringWithFormat:#"string you want to always add %#",textFeildText];
return NO;
}
This won't always show up when the user is editing the text box, but once they hit return it will automatically be added to the text box. If you use this code I recommend that you do not have the string that you want in the text field by default, or else you may end up with something like this:
String you want to add String you want to add Hello World!
If you just use the code I give you and don't put the string that you want to always be in the field by default, you're good to go! If you want to make the keyboard automatically disappear after they tap return just add this in the textFeildShouldReturn method above the 'return NO' statement:
[textFeild resignFirstResponder];

Resources