How to detect autocorrect state of UITextView? - ios

How can I tell whether UITextView is in autocorrecting state? That is, the incorrect or incomplete part is highlighted and the candidate is popped up.
Neither selectedRange nor markedTextRange is useful for this.

I suppose you could force the autocorrect to be applied and then check to see if there are any changes (a modified version of this answer: https://stackoverflow.com/a/13012198/953105), however that will destroy the autocorrect suggestion. I'm not sure of your use case, but that could work.

Related

Swift UIButton - How to remove underline?

I have problem when in iOS settings is enabled this setting "Button Shapes"
It causing this underline in application (first picture with enabled setting, second without)
Any idea how to programatically or in storyboard disable it?
I tried attributed text but I get same result :(
I'm newbie in Swift.
Thanks for help!
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
This is an answer by user4291543 from this question Remove underline on UIButton in iOS 7
[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
I found this answer works with SWFrameButton
And for all the others saying "Don't Do This", SWFrameButton is a very good example of when you would want to do this. I also think the OP's situation is a perfectly valid scenario as well...
I totally agree with #maddy's comment:
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
But I did stumble on a way to accomplish the task at hand...
In addition to a UIButton, you'll also need to make a .png file that contains nothing (meaning the entire contents have an opacity of 0%). Go ahead and load that into your xcode project's assets.
Now go ahead and set the Button's Background to that image you just provided. (In my case, I called it clear) This will remove the underline from the button's text. However, now you can't see the boundaries of the button. This can be solved by changing the Background of the button's View. Go ahead and select any color for the View's Background property and now the background of the View visibly defines the button's boundaries. You're able to see this because your clear.png has an opacity of 0%.
see the Attributes inspector for UIButton here.
Rather than trying to defeat the underline by going to make a label perform some action via UITapGestureRecognizer, this allows you to still use a UIButton. Keeping inline with accessibility features to mark buttons for people that want to do that.
You could create a custom button class with a label (with clear color). If you set the text of this label instead it shouldn`t get an underline.
Are you sure you want to do that?
Apple added an accessibility feature to mark buttons for people that want to do that. Apple will probably reject your app because it defeats a system function meant to help the disabled.
I found the solution. All you have to do is set a picture as the background of the button. just pick a picture with the same color as the button you created.

How to get Range value of text in a UITextView from tapped location?

I have a UITextView with some content of text. I need to get the exact range value of character/string from tapped location. I set TapGestureRecognizer for UITextView. In its action method i need to get the Range of tapped location. How its possible? I tested with some answers from Stackoverflow but they were not perfect. Thanks in advance.
Have you looked at the UITextView documentation?
https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView/selectedRange
You can use this selectedRange method to determine the range of text that is selected by the user.
Late edit: this was just recently downvoted, and gave me a chance to re-read my answer and I'd downvote my answer from 3 years ago too.
Updated answer:
If there is a single tap, the tap action itself should have the info you need. At the core here, you'll need the UITouch events themselves, such as to calculate where the touch occurred in relation to where your text should be. This will let you know what part of text was covered by the user's finger.
I'm honestly not sure if any 3rd party solutions exist for this, but I'd bet cocoapods.org would be a good place to start with keyword UITextView tap, if not Github itself with keywords: cocoapod uitextview.
If I were implementing such a solution, with UITouch events in hand, using the properties described in the docs I'd use The location of the touch within the view or window
given by the class.
By manipulating on one hand CGPoints from: https://developer.apple.com/documentation/uikit/uitouch/1618116-location, you can then use String mechanisms for determining size of text in your given font.
While this solution is for a UILabel, it can either:
(a) be used in a different way but hopefully quite similarly in a UITextView
OR
(b) I have seen a dummy UILabel get used simply to calculate text sizes in features requiring this kind of precision. This is usually quite effective, since changing the string of a UILabel isn't expensive and a UILabel has built in properties for size calculation based on text and font without needing to do a layout pass.
Again, apologies if the first original answer seemed sassy, it does come off that way I see now. I will confirm, though, that my original answer still stands (the selectedRange mechanism) SHOULD the desired solution be in terms of a long press. For a custom solution based on a single touch down event, though, collecting UITouch events through a delegate callback and then processing those with knowledge of what String is present is the best approach from what I can tell.

iOS Turn on capitalization on UIKeyBoard temporarily?

I need to manually turn caps lock on, on the keyboard.
Using autocapitalizationType property won't do the job. The problem is I am implementing a bullet list feature, where after the user inserts a bullet I need to turn on the capLock right away. This doesn't happen using standard autocapitalization because the first letter in my paragraph is #"•" rather than the text the user needs to enter.
Any way to turn capitalization on manually?
I had a similar issue a while back and what I ended up doing was using the UITextFieldDelegate or UITextViewDelegate depending on which control you are using they both contain textField:shouldChangeCharactersInRange:replacementString: or textView:shouldChangeCharactersInRange:replacementString: which is called every time the content of the field is changed. This allows you to change the character values automatically.
Hope this helps.
You can check out the documentation here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString:

Set voice over focus on UITextView

How can I set voice over focus on my element (such like UITextView) in iOS 6. Is it possible?
Very simple. Just do this:
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, textView);
Note that it may not work, depending on where and when you use it. If you're trying to set the focus to a particular element from viewDidLoad, the code probably won't work correctly. This is because VoiceOver makes its own attempt to set the VoiceOver focus on an element initially; if VoiceOver does this before it processes your attempt to set focus, your code will work. But if VoiceOver gets around to setting the initial focused element after it processes your attempt, your code will appear to fail.
A somewhat more reliable way to do this in viewDidLoad is to use performSelector:withObject:withDelay:, to ensure that your call is processed after VoiceOver's initial focus setting.

Curious Warning Message on UITextView

Has anyone run across this warning message building for the iPhone?
More importantly do you understand how to fix it?
"unsupported configuration data detection and editable"
It's seems to be the UITextView that is complaining.
Here's a screenshot.
The problem is that you have that textview set both to editable + to detect/autolink phone numbers, events, addresses, etc. a text area can either be editable and not detect/autolink text, or it can autolink text but not be editable.
Your settings for that textview should look like:
or
but not like:
I think in your scenario, the text input is only used to input text, nothing more. Then when it get's presented back, the "presenting text view" will take care of detecting the potential information... dates, events, etc.
To be more precise : in a simple app scenario, a user types in some text (let's say an event input text view - with no detection necessary at this point). Then when it get's eventually presented back to him or another user (let's say the detail view of the event), the text will be presented back in a "non-editable" text view that in turn will be able to have detections.
I know this question is a little old, but this is how I resolved it;
In Interface Builder I have Links Detection selected, and Editable Behaviour not selected.
Then, in my ViewController, I implemented the UITextView - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { } delegate method and return NO.
It removed the warning and prevents the user from being able to edit the UITextView's content.

Resources