NSAttributedString 对齐不适用于 html 内容

NSAttributedString alignment not working on html content

想要更改 html 标签的对齐方式。什么都不管用。我在 HTML 中没有看到任何 CSS。没有进一步的设置更改对齐方式。我还在 UILabel 上直接设置了左对齐。我错过了什么?

代码在 UILabel 扩展中。

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

NSDictionary *attr = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,  NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding), NSParagraphStyleAttributeName: paragraphStyle};
NSError *error = nil;

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

// also tried with this
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle
                           range:NSMakeRange(0, html.length)
     ];

self.attributedText = attributedText;

编辑

好的,所以我创建了一个单独的项目,在那里,文本默认左对齐。我必须找出为什么在上面的例子中文本显示为合理的。无论如何,主要问题仍然存在,我无法更改默认对齐方式。它在使用 initWithString 初始化属性字符串时起作用。但在这种情况下 html 不起作用。当 initWithData 时,如上例所示,html 有效但对齐无效。

运行 这个片段,你会看到。

UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(50, 50, 300, 400);
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentRight;

NSString *text = @"sfsf sldjfs fj <b>dsfjslf</b> dsfslkf jsfsfjsdkfsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkflsfsf sldjfs fj dsfjslf dsfslkf jsfsfjsdkfll END";

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

UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:14];
NSDictionary *attr = @{
                       NSFontAttributeName: font,
                       NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                       NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding), 
                       NSParagraphStyleAttributeName: paragraphStyle};
NSError *error = nil;

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

[attributedText addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedText.length)];

NSLog(@"err: %@", error);

label.attributedText = attributedText;

[self.view addSubview:label];

编辑 2:

案例 1 - 已解决 - 我稍后在代码对齐的某个地方进行了更改。没看到。

案例 2 - 已解决 - 查看答案。

好吧,我找到了解决办法。我假设对齐不起作用,因为它需要 html 中的这种属性。所以我将它添加到 html:

text = [@"<style> p {text-align: right;}</style><body>" stringByAppendingFormat:@"<p>%@</p></body>", text];

适用于居中、左侧等

问题是 doc initWithData:options:documentAttributes:error: 只接受 3 个可能的键:NSDocumentTypeDocumentAttributeNSCharacterEncodingDocumentAttributeNSDefaultAttributesDocumentAttribute

然后,对于您的代码段,您没有像之前那样在最后应用 NSParagraphStyleAttributedName

因此,根据您的代码段,我将这一行改为:

[attributedText addAttribute:NSParagraphStyleAttributeName
                       value:paragraphStyle
                       range:NSMakeRange(0, attributedText.length)];

并从 attr 中删除了 2 个无用的属性:

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