RTL(阿拉伯地區(qū)適配經(jīng)歷)
- 關(guān)于UIView的frame適配
尋找了很多方法,也嘗試了很多種方法,最終有了一知半解的理解,應(yīng)用到項(xiàng)目當(dāng)中的時(shí)候發(fā)現(xiàn)會(huì)有很多問題,折磨了一周,突然發(fā)現(xiàn)是在項(xiàng)目中的一個(gè)擴(kuò)展影響到了布局的改變。看圖:

UIView擴(kuò)展
在這里要注意left和right。如果你是用的Auto的話你就不能用left、right了你需要改成leading 、trailing這樣的話系統(tǒng)就會(huì)自動(dòng)適應(yīng)(我項(xiàng)目中用的不是Auto,所以我沒有嘗試)我項(xiàng)目中用的是frame的寫法,系統(tǒng)就非常不友好了。需要自己處理。
- 關(guān)于圖片鏡像
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));
這個(gè)方法返回的UIImage
- 在Access圖片資源中更改圖片的屬性

圖片模式
- 關(guān)于UIButton的內(nèi)邊距
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)];
}
- 關(guān)于聊天室聊天區(qū)域的消息適配
在聊天室公屏聊天區(qū)域展示的是用戶的昵稱以及發(fā)送的文字,表情,禮物等。具體展示如下圖:
聊天室截圖
*在這個(gè)地方存在很多問題(Auto除外):圖文混排我是用的YYText框架。正常環(huán)境下展示沒有任何問題。當(dāng)且切換語(yǔ)言到阿拉伯語(yǔ)的時(shí)候所有的布局如上圖所示。
1.在RTL環(huán)境中冒號(hào)(:)和型號(hào)()會(huì)影響數(shù)據(jù)排列的循序。所以我的處理方式是把冒號(hào)(:)和星號(hào)()用別的字符給替換掉。這里就要用到運(yùn)行時(shí):通過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]];
}
- 關(guān)于UITableView,UICollectionView,UIScrollView就需要我們單獨(dú)處理,通過查找文檔發(fā)現(xiàn)。
UITableView不用處理,只需要更改Cell的UI順序即可.
UICollectionViewFlowLayout有兩個(gè)代理方法需要實(shí)現(xiàn):
-(UIUserInterfaceLayoutDirection)effectiveUserInterfaceLayoutDirection {
if (KLUSERINFO.isRTL) {
return UIUserInterfaceLayoutDirectionRightToLeft;
}
return UIUserInterfaceLayoutDirectionLeftToRight;
}
- (BOOL)flipsHorizontallyInOppositeLayoutDirection {
return YES;
}
對(duì)于UIScrollView我的做法是把所有的數(shù)據(jù)源翻轉(zhuǎn),其它的什么也不用處理。
最后最重要的是更改視圖的顯示順序:
在這里系統(tǒng)有幾個(gè)屬性需要處理:
1.RTL模式下
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
2.正常模式下
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
