I’m interested in setting some text into a UILabel, and depending on the directionality of the language (e.g., Hebrew – right-to-left [RTL], English – left-to-right [LTR]) set the alignment of the UILabel.
Note that using iOS 6’s NSTextAlignmentNatural does not solve the problem, as it chooses alignment according to the current locale, experiments show.
Ended up following the advice given in this SO answer: write a short script that will parse the Unicode data publicly available here, and generate code to identify whether a code-point has a strong R or AL directionality attribute. Then, the string is searched for the first such character.
This is exactly what ubidi_getBaseDirection from the ICU package does.
Since the internal representation of
NSStringis UTF16 (which is a variable-length encoding), it is first converted to UTF32 in order to simplify the scanning code. An alternative approach would be to decode the string on the fly, which requires dealing with BOM and Unicode surrogates. Yet another approach is simply ignoring characters not representable by oneunichar. For more details, see Wikipedia’s UTF16 article.Short Answer
Longer answer: Generating the functions
isCodePointStrong{RTL,LTR}Create a script
hex_numbers_to_dec_ranges_py,(code shamelessly stolen from this excellent answer at StackExhange’s Code Review).
Run from a terminal:
EDIT: As @masmor correctly noted, the
forloop ingetBaseDirectionscans characters, and not bytes. Therefore, it should terminate after “character” number of iterations, and not “bytes” number of iterations. In other words,self.lengthtimes and notutf32data.lengthtimes. The code is now corrected.