2

I have an html content that i want to show on UILabel.

NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ 
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} 
documentAttributes:nil error:nil];
return attrStr;

This works good for English Language (LTR semantic), but since i want to support arabic as well the above method keeps the attributed string LTR. Inspite of using this code : self.view.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

I've tried

NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc]initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
[mutableAttributedString addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)] range:NSMakeRange(0, string.length)];
return mutableAttributedString;

Still the same. Any suggestions are welcome. Thanks

STF
  • 1,485
  • 3
  • 19
  • 36
Raj D
  • 205
  • 3
  • 16

3 Answers3

4

Hope will helpful to you for right side alignment!!

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentRight;

NSDictionary *attr = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                   NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};

NSError *error = nil;

NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc]initWithData:[text dataUsingEncoding:NSUTF8StringEncoding] options:attr documentAttributes:nil error:&error];

[attributedText addAttribute:NSParagraphStyleAttributeName
                   value:paragraphStyle
                   range:NSMakeRange(0, attributedText.length)];
return attributedText;
BuLB JoBs
  • 841
  • 4
  • 20
2

Swift version of @BuLB JoBs's answer:

let yourHTMLString = "" // your string
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
    .documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: NSNumber(value: String.Encoding.utf8.rawValue)
]
guard let data = yourHTMLString.data(using: .utf8) else { return }
var attributedText: NSMutableAttributedString!
do {
    attributedText = try NSMutableAttributedString(data: data, options: options, documentAttributes: nil)
} catch { return }

// This attribute fixes the alignment problem for Arabic language
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = .right
attributedText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedText.length))
Enes F.
  • 406
  • 6
  • 17
0

Question was about text direction, but answer about alignment. Strange...

Recently got same issue, when language direction was incorrect for attributed string from html. Fixed with html attribute dir="rtl".

See: https://www.w3schools.com/tags/att_global_dir.asp

Kirow
  • 1,077
  • 12
  • 25