Saturday, May 11, 2013

NSLocale characterDirectionForLanguage example ios


characterDirectionForLanguage:

Returns the character direction for the specified ISO language code.
+ (NSLocaleLanguageDirection)characterDirectionForLanguage:(NSString*)isoLangCode
Parameters
isoLangCode
The ISO language code.
Return Value of [NSLocale characterDirectionForLanguage]
Returns the character direction for the language. See “NSLocaleLanguageDirection” for possible values. If the appropriate direction can’t be determinedNSLocaleLanguageDirectionUnknown is returned.
Example of [NSLocale characterDirectionForLanguage]
+ (BOOL)isRtl
{
    static BOOL isRtl = NO;
    static BOOL isRtlFound = NO;
    if (!isRtlFound)
    { // This is "safe enough". Worst case is this code will be called twice in the app's lifecycle...
        isRtl = [NSLocale characterDirectionForLanguage:[NSBundle mainBundle].preferredLocalizations[0]] == NSLocaleLanguageDirectionRightToLeft;
        isRtlFound = YES;
    }
    return isRtl;
}
Example of [NSLocale characterDirectionForLanguage]
- (BOOL)isRTL {
  return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
}

-(void) changeLeftAllignment
{
    myTextView.textAlignment = (![self isRTL]]) ? UITextAlignmentLeft : UITextAlignmentRight;
}

-(void) changeRightAllignment
{
    myTextView.textAlignment = (![self isRTL]]) ? UITextAlignmentRight : UITextAlignmentLeft;
}
Example of [NSLocale characterDirectionForLanguage]
- (NSArray *)sortByUIViewOrigin {
    NSLocaleLanguageDirection horizontalDirection = [NSLocale characterDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    NSLocaleLanguageDirection verticalDirection = [NSLocale lineDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
    UIView *window = [[UIApplication sharedApplication] delegate].window;
    return [self sortedArrayUsingComparator:^NSComparisonResult(id object1, id object2) {
        CGPoint viewOrigin1 = [(UIView *)object1 convertPoint:((UIView *)object1).frame.origin toView:window];
        CGPoint viewOrigin2 = [(UIView *)object2 convertPoint:((UIView *)object2).frame.origin toView:window];
        if (viewOrigin1.y < viewOrigin2.y) {
            return (verticalDirection == kCFLocaleLanguageDirectionLeftToRight) ? NSOrderedDescending : NSOrderedAscending;
        }
        else if (viewOrigin1.y > viewOrigin2.y) {
            return (verticalDirection == kCFLocaleLanguageDirectionLeftToRight) ? NSOrderedAscending : NSOrderedDescending;
        }
        else if (viewOrigin1.x < viewOrigin2.x) {
            return (horizontalDirection == kCFLocaleLanguageDirectionTopToBottom) ? NSOrderedDescending : NSOrderedAscending;
        }
        else if (viewOrigin1.x > viewOrigin2.x) {
            return (horizontalDirection == kCFLocaleLanguageDirectionTopToBottom) ? NSOrderedAscending : NSOrderedDescending;
        }
        else return NSOrderedSame;
    }];
}