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:
!hello
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: