OGeek|极客世界-中国程序员成长平台

标题: ios - 对齐的阿拉伯文或波斯文文本或从右到左的任何语言方向 - iOS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:48
标题: ios - 对齐的阿拉伯文或波斯文文本或从右到左的任何语言方向 - iOS

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



Best Answer-推荐答案


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

NSTextAlignmentJustifie

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

- (NSMutableAttributedString *)MakeJustified_TextNSString *)text FontUIFont *)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4