Configuring keyboard settings within PDFView form fields - ios

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];
}
}
}

Related

Keep text in UITextView highlighted when the popover is dismissed

I have a UIPopover that shows up a plain view containing a UITextView filled with some text. I have managed to highlight the text. When the popover is dismissed, and re-opened, the highlight disappears. I want to keep the text highlighted even if if the application is closed. Any ideas how to achieve that?The code i used is the following :
- (void)highlight {
NSRange selectedRange = self.textViewAll.selectedRange;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithAttributedString:self.textViewAll.attributedText];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:selectedRange];
// [highlightedRange addObject:];
// This is where i tried to save each location and length in a mutable array but didn't work
[highlightedRangeLocation insertObject:[NSNumber numberWithInteger:selectedRange.location] atIndex:indexOfHighlight];
[highlightedRangeLength insertObject:[NSNumber numberWithInteger:selectedRange.length] atIndex:indexOfHighlight];
///////////////////////////////////////////////////////////////////////////////
self.textViewAll.attributedText = attributedString;
indexOfHighlight ++ ;
}
- (void)viewDidLoad {
UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:#"Highlight" action:#selector(highlight)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:highlightMenuItem]];
float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if (sysVer >= 8.0) {
self.textViewAll.layoutManager.allowsNonContiguousLayout = NO;
}
}
Could anyone point out how to continue from here?
Edit 1 :
The code that close the popover :
- (IBAction)closeFun:(id)sender {
// self.popoverPresentationController set
[self dismissViewControllerAnimated:YES completion:nil];
// [self dismis]
}
Can't you juste save the Highlighted text range in [NSUserDefaults standardUserDefaults] whenever the popover is dismissed, and retrieve it when the popover reappears ?
I think the problem is in the fact that the popover is responsible for the highlighted state, i.e .it is the popover who keeps that fact/state.
The popover is a part of presentation layer / user interface. Surely the highlight represents some fact that ( now comes the catch ) - is completely independent of the popover.
For example highlighting a task could represent that the task is due. Or, highlighting a label to red color could mean that the balance in the bank is in negative numbers.
You see, no matter what user interface element you use, they only represent some underlying business reality.
But what probably happens you create a popover instance, you set it to have a highlighted element. But then this concrete popover instance dies, when it is closed.
And the highlight dies with it.
When you click some button (I guess), a popover shows up, but it is a different instance. This instance doesn't know about highlight.
Even if you somehow managed to keep the one instance of popover alive, and just hide and show it again, the popover should NOT be responsible to know whether something is red or due, (and thus highlighted.)
In you application, you should have a well separated model layer...which is basically a set of related objects that represent state ie. fact that are related to what the application solves from business perspective (for ex. draws lines, calculates interest..stores music..anything really). This model layer, some object in it, should store the facts..ie.e. the task is due, or the balance is low.
Every time you show your popover, you should investigate what are the underlying facts in your model layer right when the popover is being shown. Ivestigating means find a programmatic way to look into model objects, find out about values there and and set up the highlight in that moment again based on this "investigation". You should not rely on the fact that it was highlighted in the not so distant past.

iOS Programming:Correcting Text

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;
}

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];
}
}

How to get the cursor in an UITextView to reflect an updated typingAttributes for paragraph alignment?

I am using a UITextView to enter some rich text. I have created a button to change the paragraph alignment (left, center, right).
I am applying the alignment to the attributedText when the user selects some text and it works as expected.
However, when the user hit return and is in a new, zero-length paragraph (just after a newline and nothing following), I believe I should change the typingAttributes to reflect the attributes I want the new text to receive.
I used the following code:
if ((paragraphRange.length == 0) && (paragraphRange.location == [mutableText length])) {
NSMutableParagraphStyle * mutableParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[mutableParagraphStyle setAlignment:textAlignment];
[mutableParagraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSMutableDictionary * attributes = [self.typingAttributes mutableCopy];
[attributes setObject:mutableParagraphStyle forKey:NSParagraphStyleAttributeName];
self.typingAttributes = attributes;
It does apply the paragraph alignment once the character is typed, but the cursor in the UITextView does not reflect the change until after the character is typed. I am looking for a mechanism to get the cursor into the right place before the text is typed. For example, if the user selects 'centered', I want the cursor to move to the center of the view to show where the text will do.
Anyone have any ideas on how to do this?
Thanks in advance,
Charlie
For those tracking this, it looks like this is a bug in iOS7. The renderer does not appear to take into account the state of the typingAttributes paragraph alignment when computing the location of the cursor.
Looks like this one needs a bug report to Apple ;-)
Here is how I am doing it and this causes the cursor to move immediately. I think the key is to set the attribute on the whole paragraph. There does seem to be a bug where sometimes the cursor does not move but if you scroll the textview then that causes it to move. Seems to only occur near the top of the uitextview (lines 1 - 5) and if the two styles use the same font size.
There is a similar bug if you select text and change it to or from bold the selected area is not resized to reflect the new width of the changed text. Also if you programatically change the Font to bold for a paragraph then the cursor position is not updated to fit the changed width of the text. However if you also change the text point size the cursor is correctly repositioned for the next text width.
/*! Applies the Normal style to the range returned by [self rangeForUserParagraphAttributeChange]
#param sender The id of the control sending the message.
*/
- (IBAction) styleNormal:(id)sender
{
FLOG(#"styleNormal called");
NSRange charRange = [self rangeForUserParagraphAttributeChange];
if (charRange.location == NSNotFound) return;
NSTextStorage *myTextStorage = [self textStorage];
if ([self isEditable] && charRange.location != NSNotFound)
{
[myTextStorage beginEditing];
[myTextStorage setAttributes:[self normalStyle] range:charRange];
[myTextStorage endEditing];
}
[self setTypingAttributes:[self normalStyle]];
}
- (NSRange)rangeForUserParagraphAttributeChange {
NSRange paragaphRange = [self.textStorage.string paragraphRangeForRange: self.selectedRange];
return paragaphRange;
}
Did you try resigning the textView as first responder, then making it first responder right after that.
[textView resignFirstResponder];
[textView becomeFirstResponder];
Calling [textView setNeedsLayout] seems to fix the cursor location.

UITextField won't becomeFirstResponder

I'm having a problem getting a textfield to accept the becomeFirstResponder directive.
I'm providing a custom mechanism to create a title in the navigation bar. I have another viewcontroller that is successfully using this same technique. On viewDidAppear I fire off:
- (void)addTitleTextField
{
CGRect textFrame = self.parentViewController.navigationController.navigationBar.frame;
textFrame.size.width = 300.0;
textFrame.origin.y = (768.0 - 300.0)/2;
textFrame.size.height = 30.0;
textFrame.origin.x = 7.0;
self.titleTextField = [[UITextField alloc] initWithFrame:textFrame];
self.titleTextField.placeholder = NSLocalizedString(#"New Multiple Choice Quiz", #"New Multiple Choice Quiz");
self.titleTextField.borderStyle = UITextBorderStyleRoundedRect;
self.titleTextField.font = [UIFont boldSystemFontOfSize:20.0];
self.titleTextField.textAlignment = NSTextAlignmentCenter;
self.titleTextField.backgroundColor = [UIColor whiteColor];
self.titleTextField.textColor = [UIColor blackColor];
self.titleTextField.delegate = self;
[self.titleTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
self.titleTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.titleTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
self.activeTextField = self.titleTextField;
self.parentViewController.navigationItem.titleView = self.titleTextField;
[self.titleTextField becomeFirstResponder];
}
self.titleTextField will allow me to set the text value, but if you check using canBecomeFirstResponder it returns NO. As you can see I am setting this on the parentViewController. I've tried using a delegate to attempt to get the parentViewController to set it. When I do the delegate and check whether the textField canBecomeFirstResponder it returns YES, but I'm still unable to make it accept the firstResponder order. Any ideas? The Docs say "A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder".
Are you telling the UITextField to become selector on a background thread?
[textField performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil
waitUntilDone:YES];
Rationale: Calling UIKit methods (ie updating the view) on a method other than the main thread won't work. . this could be happening. (It is not clear where the addTitleTextField method is being called from).
Is there another first responder that needs some time to resign?
[textField performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
Rationale: If another field is hanging onto first responder (or in the process of resigning it), it will give it time to clean up an resign, by waiting until the next run-loop. . . . . usually the next run-loop will be enough time for the previous responder to clean up, or you could try a short delay like 0.05.
You might need to add textField to view hierarchy:
v.addSubview(textField)
Another thing to do is to make sure the textField is enabled (check Interface Builder, then any disabling you might do through code).
I spent almost an hour trying to figure out what the problem was: I got a workaround by subclassing UITextField and overriding canBecomeFirstResponder to always return true. This forced it to finally show keyboard and edit the textField, but I noticed I couldn't tap/select/copy/paste the textField. User Interaction was enabled, and then I finally saw the Enabled not being checked...I blame Apple for not displaying any visual cue when a piece of UI is disabled (like the do with Hidden) lol

Resources