Punctuation in RTL text in Objective C

In the subtitles project I’m working on, I had the need to display Hebrew and Arabic, both RTL (right to left) languages. Having not worked with RTL text before, I wasn’t aware of the headache they can be, especially when combined with LTR text. A problem I discovered in Objective C, was that when displaying RTL text that finished with punctuation(which is considered LTF text), instead of showing at the left of the textfield(left is the end of RTL text!), it was incorrectly showing at the right. You can see how this can happen by copying the contents of the string below, and pasting it into the URL box of this browser window.

After some research into Bidirectional text, I discovered that there exists unicode characters that indicate properties of the text that follows. Through experimentation I found one of these characters forced the punctuation to display in the correct place in the textfield. So, if you know that the string contains RTL text, prefix the string with the “Right-to-Left Isolate” character(unicode character 2067).

self.label.text = [@"\u2067" stringByAppendingString: @",הם הכריחו אותה לשתות ויסקי"];

iOS development with Swift - book: https://manning.com/books/ios-development-with-swift video course: https://www.manning.com/livevideo/ios-development-with-swift-lv

Tagged with: , ,
Posted in Objective C

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: