20

Trying to display a hebrew string that starts with a number, always displays the number at the end of the string like so: 1. יום שישי בבוקר

but I need the number to be displayed at the right side of the text-

any solution to that?

It happens with UILabel & UITextField & UITextView

and trying to write the number at the left side also produce the same resault.

Playing with combinations of UITextAlignment will doesn't help.

j0k
  • 22,600
  • 28
  • 79
  • 90
Tiger
  • 383
  • 1
  • 5
  • 13

5 Answers5

23

You don't need to change any setting on UILabel, just put the character with unicode 0x200F before your string. This is the reason:

In Unicode many characters have a specific directionality, which lets the system know it has to be written, say LTR, like سلام. The paragraph usually uses the direction of its first character. That's why your string without the number is typed from right to left automatically.

Now some characters, like numbers, have "weak" directionality, so they basically take that of their surrounding. When you type "1. בבוקר", the system first sees 1, so takes the usual LTR direction. Changing the alignment won't help, as it just shifts the whole text to right, or center.

To solve this issue, Unicode has two marker characters (LTR: 0x200E, RTL:200F). These are invisible, but dictate the directionality. So while "1. בבוקר" is...

  1. בבוקר

if you type "#x200F" + "1. בבוקר" it will display like this:

‏1. בבוקר

mohsenr
  • 7,235
  • 3
  • 28
  • 28
  • Can we somehow mix up RTL & LTR in one UITextView? It seems that only the first marker character (LTR or RTL) is parsed so I'm not able to show a text which is written bidirectional (one line in LTR, the second line in RTL, and the thirs in LTR again) – konradowy Oct 22 '12 at 12:02
  • thank you, this solved my problem, but where did you get this info from, i searched a lot until i found your post. – DeyaEldeen Oct 13 '16 at 14:06
  • 1
    Thank you, this has really helped me resolve a bug which I've spent too long on. I can now close 20+ tabs ;-) – adnan Feb 09 '18 at 22:09
16

Building on Mo's great answer:

This is the code Obj-C:

NSString *RTFstr = "1. בבוקר"; //This could be any right-to-left string
NSString *directionalString = [@"\u200F" stringByAppendingString:[note text]];
[someUITextView setString:directionalString];

And it actually works...

Tal Bereznitskey
  • 2,051
  • 1
  • 20
  • 20
8

I had a slightly different problem but Mo's answer gave me the clue.

I wanted to get a LTR text (like "abcd") displayed in RTL direction ("dcba") without having to do myself the string reversing. Turns out enclosing the string between \u202E and \u202C does the trick.

I also recommend reading the following page as it gives a very good explanation of all these unicode magic:

http://www.iamcal.com/understanding-bidirectional-text/

Lvsti
  • 1,525
  • 15
  • 15
6

Swift anybody?

extension String {
    func stringByForcingWritingDirectionLTR() -> String {
        return "\u{200E}".stringByAppendingString(self)
    }

    func stringByForcingWritingDirectionRTL() -> String {
        return "\u{200F}".stringByAppendingString(self)
    }
}
Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
0

not sure if there's fancier way to do this but you might want to try something like this:

NSString *test = @"12. just a teststring";
NSString *number = [test substringToIndex: [test rangeOfString: @" "].location];
NSString *text = [test substringFromIndex: [test rangeOfString: @" "].location];
test = [NSString stringWithFormat: @"%@ %@", text, number];
// test == "just a teststring 12."
Marius
  • 61
  • 1
  • 6
  • Thank you very much but it is not the issue. It will display wrong no matter how I'll try to order it. Somthing with the hebrew display on screen. For examle : 1. יום שישי בבוקר And: י .1 .יום שישי בבוקר Will Be displayed the same way on screen – Tiger Feb 08 '10 at 14:37