How Can I set Localised Direction within application?(RTL if user select arabic, LTR is selected language is English)

前端 未结 4 641

My Application must support Arabic(right to left direction) and English(left to right direction) language, I need to set UI and based on user select language from my applica

4条回答
  •  离开以前
    2020-12-07 18:41

    @ashwin, I was trying to do the exact same thing as you were.

    I was hoping to find I could turn my app into a RTL language (right to left) just by changing something in the info.plist, appdelegate or who knows what. The idea is that someone with a LTR device can have my app in RTL, changing it within the app and with no need to restart it.

    Looks like there is not such a thing (I'll be glad to hear if anyone disagrees with this). However, I've found some options that are not that bad.

    1. It turns out you can force a specific view to be LTR or RTL. Thus you can set this property on every view of your app. The way you do it is up to you.

      self.view.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

    Reference: UISemanticContentAttribute

    1. You can always flip views horizontally until you get the desired setup.

    [view setTransform:CGAffineTransformMakeScale(1, 1)];

    Here's some code that might help you.

    void reloadRTLViewAndSubviews(UIView *view)
    {
        reloadRTLViews(@[view]);
        reloadRTLViews([view subviews]);
    }
    
    void reloadRTLViews(NSArray *views)
    {
        if (isRTL())
        {
            [views enumerateObjectsUsingBlock:^(UIView* view,
                                                NSUInteger idx,
                                                BOOL * stop)
             {
                 [view setTransform:CGAffineTransformMakeScale(-1, 1)];
             }];
        }
        else
        {
            [views enumerateObjectsUsingBlock:^(UIView* view,
                                                NSUInteger idx,
                                                BOOL * stop)
             {
                 [view setTransform:CGAffineTransformMakeScale(1, 1)];
             }];
        }
    }
    
    BOOL isRTL()
    {
        return isRTL_app();
    }
    
    BOOL isRTL_device()
    {
        BOOL isRTL = ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
    
        return isRTL;
    }
    
    BOOL isRTL_scheme()
    {
        BOOL isRTL = ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:[UIView new].semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft);
    
        return isRTL;
    }
    
    BOOL isRTL_app()
    {
        NSString *languageIdentifier = [AppDataManager sharedManager].languageIdentifier;
        NSArray *rtl_languages = @[@"ar"];
    
        BOOL isRTL;
    
        if ((languageIdentifier == nil) || (languageIdentifier.length == 0))
        {
            isRTL = (isRTL_device() || isRTL_scheme());
        }
        else if ([rtl_languages containsObject:languageIdentifier])
        {
            isRTL = YES;
        }
        else
        {
            isRTL = NO;
        }
    
        return isRTL;
    }
    
    BOOL deviceLanguageDirectionEqualAppLanguageDirection()
    {
        if ((isRTL_device() || isRTL_scheme()) && isRTL())//All RTL
        {
            return YES;
        }
        else if (!(isRTL_device() || isRTL_scheme()) && !isRTL())//All LTR
        {
            return YES;
        }
    
        return NO;//RTL-LTR or LTR-RTL
    }
    
    void transformViews(NSArray *views)
    {
        [views enumerateObjectsUsingBlock:^(UIView* view,
                                            NSUInteger idx,
                                            BOOL * stop)
         {
             [view setTransform:CGAffineTransformMakeScale(-1, 1)];
         }];
    }
    

    So if you were to change a UIViewController to be in RTL you can make all the setup so that it would suffice to:

    reloadRTLViewAndSubviews(self.view);
    

    Note: this is not the way it should be and Apple's guidelines say that the language should be change from the iOS settings. But this solution works if the language must be changed within the app and a language direction change between LTR and RTL is involved. And no app reset needed either.

    I hope it helped.

提交回复
热议问题