Justified arabic or persian text or any language direction right to left - iOS

570 views Asked by At

How can Justified Arabic or Persian text or any language direction right to left ?

1

There are 1 answers

0
Mohammad On

Justified Persian or Arabic text in app can't be able with:

NSTextAlignmentJustifie

and I can't find any clear solution for that ; so I create this method maybe other developer need to this:

- (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;}

now you can use this method in UITextView or UILabel very simple with any font you have:

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

I hope helpful.