Force RTL or LTR text direction

So we have the well known NSTextAlignment  attribute and it’s matching properties for UILabel , UITextField  etc.

The alignment attribute is good for basically attaching the text to a specific side (or to the middle); this though, does not solve the issue of the writing direction.

For example, in Latin languages, where the direction is left-to-right (LTR) the exclamation mark (!) is written on the right side (i.e., at the end of the sentence), but in RTL languages, like Hebrew or Arabic, the ! will be on the left side – since the sentence starts on the right and proceeds to the left.
This is something that we have to tell our view, otherwise we might end up with something like:
or in Hebrew:

There is no method that forces text direction since in Unicode the direction is set by the first character in the text (or more precisely – the first character that is a letter).
Usually that is all we need, but sometimes our text consists of multiple languages with different writing directions, or we might want to force the text to be written from a certain direction.

Take the following sentence for example:

בת מצווה is Bat Mitvah in Hebrew

The hebrew part (which says “Bat Mitzvah”…) is actually the beginning of the sentence, not the end of it, so the sentence is actually written from right to left, even though it’s actually an English sentence containing a Hebrew part.
To solve this issue we use the special characters in Unicode that are invisible but set the text direction when put at the beginning of the text:
0x200E  will force the text to be written LTR
0x200F  will force it to be RTL

We can use these characters to force any String to be written the way we want it, and as a handy Swift extension it will look something like this:



Custom Interactive UI-Direction-Responsive Push-Pop Animation

iOS 7 gives us an amazing out-of-the-box interactive pop animation. It looks and feels nice, but this is only the tip of the iceberg, and it still doesn’t gives us a custom animation. For that we are going to use the new API and to create a new push-pop animation, that is both custom, interactive, AND changes it’s way depending on the UI (RTL or LTR).
We start with a VC that will push a second VC, this clean version of the projection does nothing but push-pop.
InteractiveNav v1 (Clean Xcode Project)
On viewDidAppear: (Override if needed…) of AOMainViewController we will set self as delegate:

and of course declare ourselves as delegate in the .h:

Now we are going to implement one of the delegate methods:

This method suppose to return an object that is a delegate of UIViewControllerAnimatedTransitioning (If this is your first time dealing with the new custom animation API than – yes – LOTS of delegates around here…).
We will create an object that will be our delegate, let’s call it AOViewControllerTransition.
We need to declare delegate of UIViewControllerAnimatedTransitioning in it’s .h and import it to AOMainViewController.
Now all we have left to do is declare some BOOLs properties to know which animation to do and our method will be warnings-free.

The last thing we need to do is to implement 2 (non-optional) methods in AOViewControllerTransition’s .m:

If we run this now we have a nice push animation that is not only custom, but also changes direction when we switch between RTL and LTR UI!
But few things are missing – first of all – we don’t have the custom animation also when popping back, second – we now have ruined the interactive transition for popping.
Let’s first fix the pop: In AOSecondViewController use our AOViewControllerTransition the same way to get the custom animation:

    • Set self as delegate for UINavigationControllerDelegate in the .h.
    • Call #import "AOViewControllerTransition.h" in the .m.
    • Implement navigationController:animationControllerForOperation:fromViewController:toViewController: exactly the same way (Making a specific delegate object is the way to go here, i’m just being lazy).
    • Tell AOSecondViewController about the UI direction by adding a property and setting it in the push method:
    • Replace the line that sets the transition direction:

That’s it! We now have a transition animation that is both custom for push and pop, and also goes with the UI direction!

About interactive transition – We have few options to implement this, and we will go with a simple one, using the new UIScreenEdgePanGestureRecognizer that came with iOS7. We will add it to viewDidLoad in AOSecondViewController:

Than we implement handlePopRecognizer::

If we will run now, our GestureRecognizer will call and make the animation, but since we have yet to connect the animation our interactivePopTransition it won’t be interactive. What we need to do is simply add this method:

And we are done!
InteractiveNav v2 (Final Solution Xcode Project)

*To create the basic interactive transition I have used the tutorial of another great blog, and I never kept the link – If you have any idea which one it was please tell me so I can add a link to his post.


Fade UITableView’s Edges

I took this tutorial and made some changes and additions:

It now works on all tableviews – even if they are part of bigger screen.
It works regardless of the background or whatever is behind the tableview.
The mask changes depends on the position of the table view – when scrolled to top only the bottom faded, in when scrolled to bottom only top is faded…
1. Start by importing QuartzCore and setting a mask layer in your controller:

2. Add this to viewWillAppear:

3. Make sure you are a delegate of UIScrollViewDelegate by adding it in the .h of your controller:

4. To finish, implement scrollViewDidScroll: in your controller .m:

Again: most of the solution is from this tutorial in cocoanetics.


Custom localization macros

NSLocalizedString is great, but when we want to decide, depending on user choice or any other parameter, the localization for the app, Apple doesn’t really help us much in that case, and we basically need to pull the string we want from the specific NSBundle with the localization we need.

This great post from Aggressive Mediocrity helps us in that matter by creating a simple LocalizationSystem singleton-object that handles that situation. But we are lazy, and we want this to work on an existing project, with other developers that won’t like us for asking them to use a new macro/method for something they already got use to, so all we need to do is simply change the macro to the known NS one, and than ask Xcode to stop annoying us by warning about redefining what we know we just redefined:

Happy overriding! (:


Hello, World! !שלום, עולם! مرحبا، العالم

So everybody seems to be into this new thing called “blogs”… I decided I’ll give it a shot.
I’m aiming mostly to try answering challenges of writing apps that aim to both LTR (The mainstream) and RTL languages, and need to change the UI accordingly.
All the stuff I will write comes from challenges that I have faced and will face in the projects i’m working on, and will be things that have been tried and appeared to be working good. Before every (big) post I will also create a new, clean project, doing only the thing in the post, than upload that.

Last thing: English is not my native language, sorry for any mistakes (: