NSAttributedString对齐不适用于html内容 [英] NSAttributedString alignment not working on html content

查看:169
本文介绍了NSAttributedString对齐不适用于html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Want to change alignment of html label. Nothing works. I don't see any CSS in the HTML. There are no further settings changing the alignment. I also set left alignment directly on the UILabel. What am I missing?

代码是UILabel扩展名。

The code is in UILabel extension.

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可以工作,但是对齐不起作用。

Ok, so I created a separate project and there, the text is left aligned by default. I have to find out why in above example the text shows justified. In any case, the main problem remains, I can't change the default alignment. It works when initializing the attributed string with initWithString. But in this case html doesn't work. When initWithData, like in above example, html works but alignment does not work.

运行这个代码片段,你将会见。

Run this snippet and you will see.

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 - 解决了 - 我稍后在代码对齐的某个地方被改变了。没看到。

Case 1 - solved - I somewhere later in the code alignment was being changed. Didn't see that.

案例2 - 已解决 - 见答案。

Case 2 - solved - see answer.

推荐答案

问题在于如 doc initWithData:options:documentAttributes:error:只接受3种可能键: NSDocumentTypeDocumentAttribute NSCharacterEncodingDocumentAttribute NSDefaultAttributesDocumentAttribute

The issue is that as said by the doc initWithData:options:documentAttributes:error: only accept 3 possibles keys: NSDocumentTypeDocumentAttribute, NSCharacterEncodingDocumentAttribute and NSDefaultAttributesDocumentAttribute.

然后,使用您的代码段,您没有像上次尝试那样在结尾处应用 NSParagraphStyleAttributedName

Then, with your snippet, you didn't applied the NSParagraphStyleAttributedName at the end like in your previous attempt.

所以,从你的代码片段开始,我把这一行改为:

So, from your snippet, I put this line instead:

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

并从 attr 中移除2无用的归因:

And removed the 2 useless attributed from attr:

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

这篇关于NSAttributedString对齐不适用于html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆