ios - 对齐的阿拉伯语或波斯语文本或任何语言方向从右到左 - iOS

标签 ios objective-c arabic persian right-justified

阿拉伯语或波斯语文本或任何语言方向如何从右到左对齐?

最佳答案

应用程序中无法使用合理的波斯语或阿拉伯语文本:

NSTextAlignmentJustifie

我找不到任何明确的解决方案;所以我创建了这个方法,也许其他开发人员需要这个:

- (NSMutableAttributedString *)MakeJustified_Text:(NSString *)text Font:(UIFont *)font{

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setBaseWritingDirection:NSWritingDirectionRightToLeft];
[paragraphStyle setAlignment:NSTextAlignmentJustified];
[paragraphStyle setLineSpacing:7];

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: text];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attrStr.length)];

NSRange range = (NSRange){0,[attrStr length]};
[attrStr enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
    UIFont *replacementFont = font;
    [attrStr addAttribute:NSFontAttributeName value:replacementFont range:range];
}];
return attrStr;}

现在您可以在 UITextView 或 UILabel 中使用此方法,非常简单,使用您拥有的任何字体:

    UITextView *textview = [[UITextView alloc]init];
[textview setAttributedText:[self MakeJustified_Text:mytext Font:[UIFont fontWithName:@"Font_Name" size:15]]];

希望对您有所帮助。

关于ios - 对齐的阿拉伯语或波斯语文本或任何语言方向从右到左 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41986919/

相关文章:

ios - RSA 公钥生成——Swift

javascript - 处理不缓存音频的 IOS Web 浏览器

ios - 为什么 iOS AWS Cognito 登录不产生错误,但不执行其功能?

android - 在所有 Android 应用程序中格式化数字

ios - 自定义 UILabel 类 iOS

ios - [__NSArrayM intValue] : unrecognized selector sent to instance

iphone - 奇怪的 LLVM 警告 : no previous prototype for function for

iphone - 在 UIScrollView 上检测用户触摸

sql - 如何在 SQLite 中使用阿拉伯逗号选择 SQL

javascript - 在 JavaScript 中使用 Regex 处理阿拉伯语单词