Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right to left UILabels

I need to display a text using a UILabel ( can't use UIWebView ), and it sometimes contains both Hebrew and English. using UILabel's default settings the sentence gets mixed up and doesn't make sense. I have failed to found a way to make UILabel display text rtl.

Does anybody know anyway to do that, or a code that implements this?

like image 549
Niv Avatar asked Sep 29 '11 09:09

Niv


2 Answers

Have a look at this SO, it contains some info on this subject that might help you out. It seems to work for some by adding the code \u200F to the strings to be displayed.

NSString *RTFstr = "1. בבוקר"; //This could be any right-to-left string
NSString *directionalString = [@"\u200F" stringByAppendingString:[note text]];
[someUITextView setString:directionalString];
like image 151
Frank Avatar answered Nov 20 '22 00:11

Frank


This will work

-(void)fnForWritingDirection:(UILabel*)label textFor:(NSString *)stringForText{

    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: [stringForText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setBaseWritingDirection:NSWritingDirectionRightToLeft];

    [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];

    label.attributedText=attrStr;

}
like image 2
Sukeshj Avatar answered Nov 20 '22 00:11

Sukeshj