How to set Arabic text to right and English text to left on same label

喜你入骨 提交于 2019-11-29 11:41:02
Fahim Parkar

Below is what I did...

Added fonts in projects folder (english.ttf & arabic.ttf) as shown here.

In Localizable.strings added "myFont"="ACS Zomorrod"; (Arabic) & "myFont"="Armalite Rifle"; (English)

and then had those font in condition

NSString *myFont = localize(@"myFont");
NSString *cpFont = @"Armalite Rifle";
if ([myFont isEqualToString:cpFont]) {
    self.myLabel.textAlignment = NSTextAlignmentLeft; // this is for English
} else {
    self.myLabel.textAlignment = NSTextAlignmentRight; // this is for Arabic
}

Edit 1

Also you could have "myLang"="arabic"; & "myLang"="english"; in Localizable.strings and then have code as

NSString *myLang = localize(@"myFont");
NSString *myActualLang = @"english";
if ([myLang isEqualToString:myActualLang]) {
    self.myLabel.textAlignment = NSTextAlignmentLeft; // this is for English
} else {
    self.myLabel.textAlignment = NSTextAlignmentRight; // this is for Arabic
}

I will prefer the second option instead of first as tomorrow if I change the font, I would have to do changes at line NSString *cpFont = @"Armalite Rifle"; in all files.

Conditional if-else

Check for your font and change the alignment.

Or,

Make two labels for each of them setting their alignemnt Left-Right and Right-Left and show the text on either by checking the language.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!