Wednesday, January 10, 2018

cocos2d - Right-To-Left Text Direction in Cocos2dx



I want to use Persian/Arabic text beside English in whether Label or ui::Text.


Persian is a Right-to-Left and English is a Left-to-Right language and so I have problem to combine them in one string.


Scnario:


I want to show this text: 1."من1" then 2."emadpres2" and finally 3."3هستم".


If I write this text in a Left-to-Right editor it show as : ( Wrong Direction: =====> )



1من emadpres2 هستم3



But actually it should be shown as : ( Correct Direction: <===== )




هستم3 emadpres2 من1



This is my code :


auto rtl = Label::createWithSystemFont("سلام emadpres چطوری", B_NAZANIN, 25, Size(480, 100));
// The string here is also in wrong direction because the Visual Studio editor is Left-to-Right.
rtl->setColor(Color3B(0, 0, 0));
rtl->setPosition(...);
rtl->setHorizontalAlignment(TextHAlignment::RIGHT);
this->addChild(rtl, Z_NORMAL);


and this is the screenshot of this code:


enter image description here


As you see, I tried to use TextHAlignment but It doesn't help. I need something to change Direction not Alignment .


How should I make it Right-to-Left ?


PS: Cocos2dx v3.3



Answer



It's not the label that understand the text direction, but the OS. as you guessed Android check the first letter to guess how it should format the text, whether it's RTL or LTR.


Label::createWithSystemFont basically asks the OS to render the text for you. It's completely out of cocos2dx's control how the text is rendered, And thus it's completely OS dependent. If you want to write a code, which works on every platform, you should handle things yourself. Meaning you should separate the string over it's spaces (and direction independent characters e.g. punctuation marks) and place them carefully, one by one when they should be shown.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...