NSMutableParagraphStyle ignores NSWritingDirectionNatural, defaults to LTR for Arabic text

I am using NSAttributedString UIKit Additions to draw an attribute string in a UIView. The problem is that despite using the NSWritingDirectionNatural value for the NSWritingDirectionNatural property of my paragraph style, the text always defaults from left to right.

This is how I form the sent string (simplified):

 NSString *arabic = @"العاصمة الليبية لتأمينها تنفيذا لقرار المؤتمر الوطني العام. يأتي ذلك بعدما أعلن اللواء الليبي المتقاعد خليفة حفتر أنه طلب من المجلس الأعلى للقض الدولة حتى الانتخابات النيابية القادمة"; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.baseWritingDirection = NSWritingDirectionNatural; paragraph.lineBreakMode = NSLineBreakByWordWrapping; NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; attributes[NSParagraphStyleAttributeName] = paragraph; NSAttributedString *string = [[NSAttributedString alloc] initWithString:arabic attributes:attributes]; لتأمينها تنفيذا لقرار المؤتمر الوطني العام يأتي ذلك بعدما أعلن اللواء الليبي المتقاعد خليفة حفتر أنه طلب من المجلس الأعلى للقض الدولة حتى الانتخابات النيابية القادمة."; NSString *arabic = @"العاصمة الليبية لتأمينها تنفيذا لقرار المؤتمر الوطني العام. يأتي ذلك بعدما أعلن اللواء الليبي المتقاعد خليفة حفتر أنه طلب من المجلس الأعلى للقض الدولة حتى الانتخابات النيابية القادمة"; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.baseWritingDirection = NSWritingDirectionNatural; paragraph.lineBreakMode = NSLineBreakByWordWrapping; NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; attributes[NSParagraphStyleAttributeName] = paragraph; NSAttributedString *string = [[NSAttributedString alloc] initWithString:arabic attributes:attributes]; 

And this is how I draw the text:

 - (void)drawRect:(CGRect)rect { [self.attributedText drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin context:nil]; } 

And yet it still flows from left to right:

screenshot

What am I missing?

+6
source share
3 answers

I do not believe that the recording direction will be automatically configured for you using baseWritingDirection if you do not switch languages ​​on the device:

"If you specify NSWritingDirectionNaturalDirection, the receiver will allow recording direction to either NSWritingDirectionLeftToRight or NSWritingDirectionRightToLeft, depending on the direction in which user preferences are set.

For some reason, text that still doesn’t work for you, even with Arabic selected, without adding a language to supported localizations. This character seemed to work without doing this for me: كتب

In addition, it seems that Xcode is changing characters in hard-coded Arabic lines so that you can screw part of this copy and paste.

You can use the agiletortoises sentence or the NSLinguisticTagger Language tag scheme to manually set the language.

+5
source

I can’t explain why it doesn’t work the way you wrote it, but I used a solution to explicitly indicate the direction based on the well-known RTL languages, which used this as a starting point:

fooobar.com/questions/943923 / ...

+4
source

I don’t know if this is connected or not, but when I tested the support from right to left in Auto Layout, it did not work until I added localization for this language (Arabic, Hebrew, etc.) to the application.

0
source

Source: https://habr.com/ru/post/969705/


All Articles