Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString alignment not working on html content

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.

like image 507
User Avatar asked Mar 02 '15 18:03

User


1 Answers

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)};
like image 60
Larme Avatar answered Oct 05 '22 23:10

Larme