7. Show Arabic text on UILabel, UITextView, etc.

I got stuck for this requirement when I was getting response from server in two languages, English and Arabic, for English it was giving normal text but for Arabic, it was returning text something like this…

نريد أن نرى هذا في اللغة العربية

So then I figured out from ‘Stackoverflow.com’ that these are code for arabic font and we have convert it into respective font strip using proper encoding, which is as follows:

int c = ... /* your 4 text digit unicode ordinal converted to an integer */
charString = [ NSString stringWithFormat:@"%C", c ];
//originally posted by hotpaw2 at this link.

But its just one char what if you want to show full sentence so I have made a small utility which will take arabic codes as input and will return string containing arabic font, as follows:

-(NSString*)toArabic:(NSString*) arabicCode
{
 NSArray *wordsArray = [arabicCode componentsSeparatedByString:@" "];
 NSString *sentence = @"";
 for(int i=0;i<[wordsArray count];i++)
 {
 NSString *word = [wordsArray objectAtIndex:i];
 word = [word stringByReplacingOccurrencesOfString:@"&#" withString:@""];
 NSString *arabicForm = @"";
 NSArray *charArray = [word componentsSeparatedByString:@";"];
 for(int j=0;j<[charArray count];j++)
 {

 int code = [[charArray objectAtIndex:j] intValue];

 if(code!=0)
 arabicForm = [arabicForm stringByAppendingFormat:@"%C",code];

 }
 sentence = [sentence stringByAppendingFormat:@" %@",arabicForm];

 }
 //NSLog(@"%@",sentence);
 return sentence;

}

How will it work?


NSString *arabicString = [self toArabic:@"&#1606;&#1585;&#1610;&#1583; 
&#1571;&#1606; &#1606;&#1585;&#1609;   &#1607;&#1584;&#1575; 
&#1601;&#1610;   &#1575;&#1604;&#1604;&#1594;&#1577;   
&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;"];
NSLog(@"Arabic text:%@",arabicString);

Hope it can help you somewhere.

Let me know for any confusion....

2 Responses

Leave a Reply