I want to determine the writing direction of a string so that I can render Right-to-Left languages such as Arabic correctly in a 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;
}
Works fine but feels a little heavy just for the purpose of discovering which way to align my text especially as its been called in drawInContext (although relatively infrequently).
Is there a lighter way of determining the writing direction for a given string or should I just stick with this under the basis of premature optimisation. And its got to be iOS 5 friendly.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…