我有一个要在 UILabel 上显示的 html 内容。
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
documentAttributes:nil error:nil];
return attrStr;
这适用于英语语言(LTR 语义),但由于我也想支持阿拉伯语,所以上述方法保留了属性字符串 LTR。
尽管使用此代码:self.view.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
我试过了
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;
还是一样。欢迎任何建议。
谢谢
Best Answer-推荐答案 strong>
希望对您右侧对齐有帮助!
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;
关于ios - NSAttributedString - 添加阿拉伯语的书写方向,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/47280999/
|