When is -[UITextInput selectionRectsForRange:] called? - ios

I have an app with a custom text editor that implements the UITextInput protocol. In iOS 6, Apple added one new required method to the protocol:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
I've implemented this, but I can't seem to find a way to trigger it. At least in the way my app works, it seems to never get called by the text system. Does anyone know what it's used for?

This method is only used by subclasses of UITextView. This is the only method that would give you the system selection and loupe. This is what I was told at WWDC.
I am working on my own DTRichTextEditor as well and I implemented it nevertheless, maybe one day we get the selection/loupes also for our own UIViews that are not derived from UITextView.

Related

Change UIAlertView with swizzling

I've recently been handed a private hardware SDK to integrate into my company's mobile app. Everything works great except they are using a UIAlertView as the result of some opaque method call, and my design team wants to brand this. I don't have access to the SDK's source code. Is there a way I can safely swizzle UIAlertView to preserve all functionality and simply modify the appearance of the UIAlertView so it's more branded/consistent with the app appearance? If so, would I overload drawRect or something else, and how would I figure out what the names of the labels in UIAlertView are so that I can draw them with the size, shape, and color that I want?
for added detail, the app does not currently use UIAlertView or UIAlertViewController so in theory swizzling would only affect whatever is going on with this closed-source SDK.
That wouldn't be straightforward at all.
If the SDK is using UIAlertView, then try swizzling the show method.
Your implementation should do approximately the following:
1) Do not let the original UIAlertView to show -- it is very hard to customize it.
2) Keep reference of old .delegate to be able to notify it when needed.
3) Create your own custom UIView and use [[UIApplication sharedApplication].keyWindow addSubview:myCustomAlertView];.
4) I believe you can get all of the previous UIAlertView variables (i.e. button titles, textFields, etc), using its properties such as
#property(nonatomic,copy) NSString *title;
#property(nullable,nonatomic,copy) NSString *message;
- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
#property(nonatomic,readonly) NSInteger numberOfButtons;
#property(nonatomic) NSInteger cancelButtonIndex;
5) Create your own designs and call corresponding delegate methods when needed.
Other approach is to use Apple's Private API, but this may lead to bad results.
This is a VERY HACKY approach. Firstly, you have no guarantees that it would even work. Secondly, your app may be rejected. Therefore, I wouldn't really recommend it...
However, if you really want to go with this approach, then try doing this:
After [myAlertView show]; look at its properties after some delay (i.e. 0.01 sec is enough):
Now look for suspicious properties (probably of UIView class), which might have UI-related information. For example, __representer looks quite interesting -- it has constraints, it has labelContainerView... Try playing with those properties.
In order to get that __representer, use KVC and KVO (i.e. start with id theAlertController = [myAlertView valueForKey:#"_alertController"];). Then dive deeper and deeper.
Hopefully, you would be able to find useful properties and would be able to change their values via KVC.

KVC in UITableView subclass causing crash with accessibility enabled

I have a custom UITableView subclass in which I override +accessInstanceVariablesDirectly to return NO in order to ensure attributes with no setter cannot be set using KVC.
When removing this table view from the view hierarchy, the app crashes - sometimes - and now for the weird part: only if Accessibility is enabled! (i.e. the Accessibility Inspector is visible, or you have Accessibility enabled on a physical device)
If I do not override +accessInstanceVariablesDirectly, everything works fine. I figured maybe UITableView relies on accessing some instance variables directly - but then what is the point of this method, if I can break superclasses by using it? Is there a way to specify this behavior per-attribute, like +automaticallyNotifiesObserversForKey:? However I am baffled by the fact that this issue only exists when Accessibility is enabled.
I tried analyzing the project with various Instruments, but without success.
You can find a minimal project reproducing the issue here. I would greatly appreciate any pointers on why this is happening or how to achieve what I want nonetheless.
This issue appears to be fixed in iOS 9.

Sending images with iOS 8 custom keyboard?

I've been developing a custom keyboard for iOS 8, but stumbled upon a problem trying to send images using the keyboard. I did some research and it seems like there isn't a simple way to do this with UITextDocumentProxy because only NSStrings are allowed.
Am I either overlooking any simple ways to use a custom keyboard to send images and/or is there any way to work around this issue?
Thanks in advance
Apparently, you are not the only person to try a keyboard like this. If you look at the animated GIF on the site, the keyboard uses copy/paste to add the image to the messages.
The UIKeyInput Protocol, which is inherited by UITextDocumentProxy, has the following function:
func insertText(_ text: String) //Swift
- (void)insertText:(NSString *)text //ObjC
These only take a String or an NSString, as you already know. Because there are no other methods to insert content, it can be assumed that, currently, there is no other way.
Right now, I would suggest that you adopt a similar usage. When the user taps on the image, add it to the UIPasteboard. Maybe present a little banner on top of the keyboard saying, "You can now paste" or something, but that would be up to you to decide.

Edit exisiting class propertys or make system wide changes to a class

I have problem with iOS 8 and the SKLabelNode Class.
I used in my Project lot of SKLabelNodes(We speak about 120 nodes) and in iOS8 Apple has changed the built in FontType and now all my Labels hardly readable. I would like to change back, but I dont want to edit all the Nodes by one. I would like to change the SKLabelNode Class Defaults Font type. Is it possible? Or any other solution?
Sounds like your best bet would be to use a category Apple Category Docs
Or take a look at swizzling. There's a good tutorial here Swizzling
I think your best bet is actually method swizzling to replace the getter or modify the category to add a new init that sets a default font. Hope this helps.

Custom Keyboard in iOS 8

iOS 8 lets us create our own custom keyboards.
Is it possible to make a custom keyboard output anything other than unattributed NSStrings?
Can I make a keyboard that outputs images to say the Messages app, or is it impossible?
No, this would be too difficult since most UITextField / UITextView can only handle NSString object.
As described in the UITextDocumentProxy only NSStrings are allowed.
UITextDocumentProxy is the communication object used by UIInputViewController. UIInputViewController seems to be the base for creating custom keyboards.
Also have a look at App Extension Programming Guide
- Custom Keyboard
You can create a twitter-style workaround: when the user selects an image, you upload it to your server and send a shortened url in reponse. this shortened url will be inserted to the text view of the host app and good luck :)

Resources