问题
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?
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;
Edit
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];
Edit 2:
Case 1 - solved - I somewhere later in the code alignment was being changed. Didn't see that.
Case 2 - solved - see answer.
回答1:
The issue is that as said by the doc initWithData:options:documentAttributes:error:
only accept 3 possibles keys: NSDocumentTypeDocumentAttribute
, NSCharacterEncodingDocumentAttribute
and NSDefaultAttributesDocumentAttribute
.
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)];
And removed the 2 useless attributed from attr
:
NSDictionary *attr = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
回答2:
Well, I found a solution. I assume the alignment doesn't work because it expects this kind of attributes in the html. So I added it to the html:
text = [@"<style> p {text-align: right;}</style><body>" stringByAppendingFormat:@"<p>%@</p></body>", text];
Works with center, left, etc.
来源:https://stackoverflow.com/questions/28816501/nsattributedstring-alignment-not-working-on-html-content