2

I am stuck to one point i want to develop a app that support LTR and RTL both but i don't want to create two .xib for LTR and RTL.

So can any one provide me any method avalable fot LTR to RTL using same xib.

thank you in advance.

Hardik Vyas
  • 1,973
  • 1
  • 20
  • 43
  • you mean without restarting the app after changing lanuage , i mean in same language context ? – Shehata Gamal Mar 13 '18 at 07:16
  • check my answer here: full background (iOS versions, cases, etc..) https://stackoverflow.com/questions/31877140/how-can-i-set-localised-direction-within-applicationrtl-if-user-select-arabic/31877713#31877713 – hasan Mar 13 '18 at 07:18
  • @Sh_Khan Yes When user select language from app i want to change all UI to RTL and i will change all text. Mean if i have button on left bottom and i set RTL language than that button should set to Right bottom exact mirror design. – Hardik Vyas Mar 13 '18 at 07:24

3 Answers3

2

There is no need to create 2 xib files in order to get LTR and RTL to work.

Basically you need to:

  1. set leading and trailing constraints
  2. If iPhone language is RTL leading will now act as trailing and trailing will act as leading (thus there is no need to create 2 xib files and your view element will be flipped)
  3. In some cases you may want to force RTL/LTR images using imageFlippedForRightToLeftLayoutDirection():

    image = [image imageFlippedForRightToLeftLayoutDirection];
    

    while checking current language using:

    BOOL isLeftToRight = [UIView userInterfaceLayoutDirectionForSemanticContentAttribute:[[self view] semanticContentAttribute]] == UISemanticContentAttributeForceLeftToRight;
    
  4. Remember that each UIView has semanticContentAttribute that can be set to force RTL UISemanticContentAttributeForceRightToLeft. i.e:

    [view setSemanticContentAttribute: UISemanticContentAttributeForceRightToLeft];
    
  5. When using textFields please use the below alignment so each RTL/LTR will work as it should be:

    enter image description here

OhadM
  • 4,687
  • 1
  • 47
  • 57
  • i want it in objective c – Hardik Vyas Mar 13 '18 at 07:29
  • Updated to objc – OhadM Mar 13 '18 at 07:37
  • 1
    Thanks for your answer. your given solution works very fine for me thank you again.... – Hardik Vyas Mar 29 '18 at 13:33
  • You can reaccept this answer if you think it solved your issue - the accepted answer is the answer that suits your needs the best. – OhadM Apr 01 '18 at 06:54
  • 1
    your solution is works for me but as Above ans is as perfect as i want so i have accepted above answer. but as your answer is also good so i have upvote your answer and comment on it so other developer can also get to know that this answer is also works. – Hardik Vyas Apr 02 '18 at 07:35
2

You can do this by changing the semantics

   self.anyView.semanticContentAttribute = .forceLeftToRight

OR

   self.anyView.semanticContentAttribute = .forceRightToLeft

In objective-c

 self.anyView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;

Or

 self.anyView.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

see also this demo : RTLDemo

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

below are a few of my methods.

  • first of all, verify if the layout direction (you could create a macro for this)

    [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft

  • use IBOutlet for NSLayoutConstraint and set constant/priority to shift your UI elements

  • for labels use NSTextAlignmentNatural for left alignment in LTR and for RTL will take care the system. Else you have to set them by yourself the text alignment
  • for some relevant images you have to flip them horizontally
  • for any kind of drawings (in drawRect or layers) you should pay attention

These are important for me, but there are more. Just be creative

Mihai D.
  • 105
  • 1
  • 4