Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

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

I learned how to make English and Arabic project from previous question I asked.

Same Project at github

Now what I did is added a label in this project and wrote "Welcome".

The problem is the layout. When I have English text it is at left side (obviously), but when the Arabic text comes, it should start from right to left. But it is aligned to left only.

Any idea how to deal with such case?

Below are the screenshots...

English

enter image description here

Arabic

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...