Saturday, May 11, 2013

NSLocale NSLocaleLanguageDirectionRightToLeft example ios


NSLocaleLanguageDirection - NSLocaleLanguageDirectionRightToLeft

These constants describe the text direction for a language. Used by the methodslineDirectionForLanguage: and characterDirectionForLanguage:.
enum {
   NSLocaleLanguageDirectionUnknown = kCFLocaleLanguageDirectionUnknown,
   NSLocaleLanguageDirectionLeftToRight = kCFLocaleLanguageDirectionLeftToRight,
   NSLocaleLanguageDirectionRightToLeft = kCFLocaleLanguageDirectionRightToLeft,
   NSLocaleLanguageDirectionTopToBottom = kCFLocaleLanguageDirectionTopToBottom,
   NSLocaleLanguageDirectionBottomToTop = kCFLocaleLanguageDirectionBottomToTop
};
typedef NSUInteger NSLocaleLanguageDirection;
Constants
NSLocaleLanguageDirectionUnknown
The direction of the language is unknown.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionLeftToRight
The language direction is from left to right.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionRightToLeft
The language direction is from right to left.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionTopToBottom
The language direction is from top to bottom.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionBottomToTop
The language direction is from bottom to top.
Available in iOS 4.0 and later.
Declared in NSLocale.h.



Example of [NSLocale NSLocaleLanguageDirectionRightToLeft]

static BOOL s_isRtl = NO;
+ initialize
{
    s_isRtl = [NSLocale characterDirectionForLanguage:[NSBundle mainBundle].preferredLocalizations[0]] == NSLocaleLanguageDirectionRightToLeft;
}

Example of [NSLocale NSLocaleLanguageDirectionRightToLeft]
+ (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 NSLocaleLanguageDirectionRightToLeft]
- (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;
}