Niv
Niv

Reputation: 2312

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?

Upvotes: 6

Views: 4564

Answers (2)

Sukeshj
Sukeshj

Reputation: 1503

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;

}

Upvotes: 2

Frank
Frank

Reputation: 3376

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];

Upvotes: 6

Related Questions