收藏iosiOS 宝典

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

2020-07-03  本文已影响0人  NSLog_57c2

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

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

  1. 在Access图片资源中更改图片的属性
图片模式

+ (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)];
}
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不用处理,只需要更改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(阿拉伯地区适配)的见解。如有不对的地方,欢迎评论。

上一篇 下一篇

猜你喜欢

热点阅读