A lighter way of discovering text writing direction

后端 未结 3 1216
南方客
南方客 2020-12-09 22:59

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 23:21

    Since UITextAlignment is deprecated, here's an NSString category with NSWritingDirection:

    - (NSWritingDirection)alignment{
        if (self.length) {
            NSArray *rightLeftLanguages = @[@"ar",@"he"];
            NSString *lang = CFBridgingRelease(CFStringTokenizerCopyBestStringLanguage((CFStringRef)self,CFRangeMake(0,self.length)));
            if ([rightLeftLanguages containsObject:lang]) {
                return NSWritingDirectionRightToLeft;
            }
        }
        return NSWritingDirectionLeftToRight;
    

    }

提交回复
热议问题