NSAttributedString - 为阿拉伯语添加书写方向
NSAttributedString - add writing direction for Arabic Language
我有一个 html 内容想在 UILabel 上显示。
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;
还是一样。欢迎提出任何建议。
谢谢
希望对您右侧对齐有帮助!!
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;
问题是关于文本方向的,但回答是关于对齐的。奇怪...
最近遇到了同样的问题,来自 html 的属性字符串的语言方向不正确。已修复 html 属性 dir="rtl"
.
Swift @BuLB JoBs 回答的版本:
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))
我有一个 html 内容想在 UILabel 上显示。
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;
还是一样。欢迎提出任何建议。 谢谢
希望对您右侧对齐有帮助!!
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;
问题是关于文本方向的,但回答是关于对齐的。奇怪...
最近遇到了同样的问题,来自 html 的属性字符串的语言方向不正确。已修复 html 属性 dir="rtl"
.
Swift @BuLB JoBs 回答的版本:
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))