RTL适配

2020-09-21  本文已影响0人  哥哥很冷

RTL(阿拉伯地区适配经历)

关于UIView的frame适配

寻找了很多方法,也尝试了很多种方法,最终有了一知半解的理解,应用到项目当中的时候发现会有很多问题,折磨了一周,突然发现是在项目中的一个扩展影响到了布局的改变。看图:

UIView扩展

在这里要注意left和right。如果你是用的Auto的话你就不能用left、right了你需要改成leading 、trailing这样的话系统就会自动适应(我项目中用的不是Auto,所以我没有尝试)我项目中用的是frame的写法,系统就非常不友好了。需要自己处理。

关于图片镜像

Thismethod cannot be used to create aleft-to-rightversion of aright-to-leftsource image,and will be deprecatedina future release.Newcode should instead use-imageWithHorizontallyFlippedOrientation to construct aUIImageAsset.-(UIImage*)imageFlippedForRightToLeftLayoutDirectionAPI_AVAILABLE(ios(9.0));

这个方法返回的UIImage

在Access图片资源中更改图片的属性

图片模式

关于UIButton的内边距

1.代码如下:

+(void)load{swizzling_exchangeMethod(self,@selector(setContentEdgeInsets:),@selector(rtl_setContentEdgeInsets:));swizzling_exchangeMethod(self,@selector(setImageEdgeInsets:),@selector(rtl_setImageEdgeInsets:));swizzling_exchangeMethod(self,@selector(setTitleEdgeInsets:),@selector(rtl_setTitleEdgeInsets:));}-(void)rtl_setContentEdgeInsets:(UIEdgeInsets)contentEdgeInsets{[selfrtl_setContentEdgeInsets:RTLEdgeInsetsWithInsets(contentEdgeInsets)];}-(void)rtl_setImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets{[selfrtl_setImageEdgeInsets:RTLEdgeInsetsWithInsets(imageEdgeInsets)];}-(void)rtl_setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets{[selfrtl_setTitleEdgeInsets:RTLEdgeInsetsWithInsets(titleEdgeInsets)];}

关于聊天室聊天区域的消息适配

在聊天室公屏聊天区域展示的是用户的昵称以及发送的文字,表情,礼物等。具体展示如下图:

聊天室截图

*在这个地方存在很多问题(Auto除外):图文混排我是用的YYText框架。正常环境下展示没有任何问题。当且切换语言到阿拉伯语的时候所有的布局如上图所示。

1.在RTL环境中冒号(:)和型号()会影响数据排列的循序。所以我的处理方式是把冒号(:)和星号()用别的字符给替换掉。这里就要用到运行时:通过hookUILabel的setText方法更改所有的文字。代码如下:

-(NSString*)RTLString:(NSString*)string{if(KLUSERINFO.isRTL){string=[string stringByReplacingOccurrencesOfString:@"*"withString:@"x"];}else{}returnstring;}+(void)load{swizzling_exchangeMethod(self,@selector(setText:),@selector(rtl_setText:));}-(void)rtl_setText:(NSString*)text{[selfrtl_setText:[selfRTLString:text]];}

关于UITableView,UICollectionView,UIScrollView就需要我们单独处理,通过查找文档发现。

UITableView不用处理,只需要更改Cell的UI顺序即可.

UICollectionViewFlowLayout有两个代理方法需要实现:

-(UIUserInterfaceLayoutDirection)effectiveUserInterfaceLayoutDirection{if(KLUSERINFO.isRTL){returnUIUserInterfaceLayoutDirectionRightToLeft;}returnUIUserInterfaceLayoutDirectionLeftToRight;}

-(BOOL)flipsHorizontallyInOppositeLayoutDirection{returnYES;}

对于UIScrollView我的做法是把所有的数据源翻转,其它的什么也不用处理。

最后最重要的是更改视图的显示顺序:

在这里系统有几个属性需要处理:

1.RTL模式下

[UIView appearance].semanticContentAttribute=UISemanticContentAttributeForceRightToLeft;[UISearchBar appearance].semanticContentAttribute=UISemanticContentAttributeForceRightToLeft;

2.正常模式下

[UIView appearance].semanticContentAttribute=UISemanticContentAttributeForceLeftToRight;[UISearchBar appearance].semanticContentAttribute=UISemanticContentAttributeForceLeftToRight;

以上就是对RTL(阿拉伯地区适配)的见解。如有不对的地方,欢迎评论。

作者:NSLog_57c2

链接:https://www.jianshu.com/p/eb54015cb774

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇 下一篇

猜你喜欢

热点阅读