GTFInternationalization
CGRects 和 UIEdgeInsets 的从右到左计算
UIView 是在其父视图的框架(CGRect)中定位的,包括一个原点和大小。当设备设置为右到左(RTL)语言的文本时,我们通常希望能够围绕垂直轴镜像界面。这个库包含了一些辅助函数来帮助修改用于 RTL 的框架和边距。
// To flip a subview's frame horizontally, pass in subview.frame and the width of its parent.
CGRect originalFrame = childView.frame;
CGRect flippedFrame = GTFRectFlippedHorizontally(originalFrame, CGRectGetWidth(self.bounds));
childView.frame = flippedFrame;
镜像图片
在 UIImage 类别中引入了 iOS 10 的 [UIImage imageWithHorizontallyFlippedOrientation]
到更早的 iOS 版本。
// To mirror on image, invoke gtf_imageWithHorizontallyFlippedOrientation.
UIImage *mirroredImage = [originalImage gtf_imageWithHorizontallyFlippedOrientation];
添加语义内容
在 UIView 类别中引入了 iOS 9 的 -[UIView semanticContentAttribute]
和 iOS 10 的 -[UIView effectiveUserInterfaceLayoutDirection]
到更早的 iOS 版本。
// To set a semantic content attribute, set the gtf_semanticContentAttribute property.
lockedLTRView.gtf_semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
// gtf_semanticContentAttribute is used to calculate the gtf_effectiveUserInterfaceLayoutDirection
if (customView.gtf_effectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
// Update customView's layout to be in RTL mode.
}
双向字符串嵌入
NSString类别提供了一个简单的API,用于将字符串包装在Unicode标记中,从而使LTR和RTL文本可以在同一个字符串中同时存在。
// To embed an RTL string in an existing LTR string we should wrap it in Unicode directionality
// markers to maintain preoper rendering.
// The name of a restaurant is in Arabic or Hebrew script, but the rest of string is in Latin.
NSString *wrappedRestaurantName =
[restaurantName gtf_stringWithStereoReset:NSLocaleLanguageDirectionRightToLeft
context:NSLocaleLanguageDirectionLeftToRight];
NSString *reservationString = [NSString stringWithFormat:@"%@ : %ld", wrappedRestaurantName, attendees];
用法
请参考示例,了解如何使用此库提供的功能的详细示例。
安装
GTFInternationalization 可以通过 CocoaPods 使用。要安装,只需将以下行添加到您的 Podfile 中
pod 'GTFInternationalization'
作者
liuxc123,[email protected]
许可
GTFInternationalization 在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。