I want to determine the recording direction of a string so that I can correctly display languages ββfrom right to left, such as Arabic, in CALayer.
so i have this method
+(UITextAlignment)alignmentForString:(NSString *)astring { UITextView *text = [[UITextView alloc] initWithFrame:CGRectZero]; text.text = astring; if ([text baseWritingDirectionForPosition:[text beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionRightToLeft) { return UITextAlignmentRight; } return UITextAlignmentLeft; }
It works fine, but it feels a little heavy just to find out how to align the text, especially because it was called in drawInContext (albeit relatively rarely).
Is there an easier way to determine the recording direction for a given line, or should I just stick to this at the heart of premature optimization. And it should become iOS 5 friendly.
source share