iOS开发心得(RTL阿拉伯适配)

RTL(阿拉伯地区适配经历)关于UIView的frame适配寻找了很多方法,也尝试了很多种方法,最终有了一知半解的理解,应用到项目当中的时候发现会有很多问题,折磨了一周,突然发现是在项目中的一个扩展影响到了布局的改变。看图:

UIView扩展在这里要注意left和right。如果你是用的Auto的话你就不能用left、right了你需要改成leading 、trailing这样的话系统就会自动适应(我项目中用的不是Auto,所以我没有尝试)我项目中用的是frame的写法,系统就非常不友好了。需要自己处理。关于图片镜像This method cannot be used to create a left-to-right version of a right-to-left source image, and will be deprecated in a future release. New code should instead use -imageWithHorizontallyFlippedOrientation to construct a UIImageAsset.- (UIImage *)imageFlippedForRightToLeftLayoutDirection API_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 { [self rtl_setContentEdgeInsets:RTLEdgeInsetsWithInsets(contentEdgeInsets)];}- (void)rtl_setImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets { [self rtl_setImageEdgeInsets:RTLEdgeInsetsWithInsets(imageEdgeInsets)];}- (void)rtl_setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets { [self rtl_setTitleEdgeInsets:RTLEdgeInsetsWithInsets(titleEdgeInsets)];}关于聊天室聊天区域的消息适配在聊天室公屏聊天区域展示的是用户的昵称以及发送的文字,表情,礼物等。具体展示如下图:

聊天室截图*在这个地方存在很多问题(Auto除外):图文混排我是用的YYText框架。正常环境下展示没有任何问题。当且切换语言到阿拉伯语的时候所有的布局如上图所示。1.在RTL环境中冒号(:)和型号()会影响数据排列的循序。所以我的处理方式是把冒号(:)和星号()用别的字符给替换掉。这里就要用到运行时:通过hookUILabel的setText方法更改所有的文字。代码如下:- (NSString *)RTLString:(NSString *)string { if (KLUSERINFO.isRTL) { string = [string stringByReplacingOccurrencesOfString:@"*" withString:@"x"]; } else { } return string;}+ (void)load { swizzling_exchangeMethod(self, @selector(setText:), @selector(rtl_setText:));}- (void)rtl_setText:(NSString *)text { [self rtl_setText:[self RTLString:text]];}关于UITableView,UICollectionView,UIScrollView就需要我们单独处理,通过查找文档发现。UITableView不用处理,只需要更改Cell的UI顺序即可.UICollectionViewFlowLayout有两个代理方法需要实现:-(UIUserInterfaceLayoutDirection)effectiveUserInterfaceLayoutDirection { if (KLUSERINFO.isRTL) { return UIUserInterfaceLayoutDirectionRightToLeft; } return UIUserInterfaceLayoutDirectionLeftToRight;}- (BOOL)flipsHorizontallyInOppositeLayoutDirection { return YES;}对于UIScrollView我的做法是把所有的数据源翻转,其它的什么也不用处理。最后最重要的是更改视图的显示顺序:在这里系统有几个属性需要处理:1.RTL模式下[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;2.正常模式下[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;以上就是对RTL(阿拉伯地区适配)的见解。如有不对的地方,欢迎评论。

(0)

相关推荐