-1

I am trying to set NSAttributed string to UILabel by passing string check the method below :

-(NSAttributedString *) returnRichTextForString:(NSString *) inputString {

   NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding]  options:@{NSDocumentTypeDocumentAttribute:  NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
   NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
   [paragrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];
   [attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [attributedString length])];
   return attributedString; 
}

And I gave input string as : @"<body> <p>This is some text.</p> <center>This text will be center-aligned.</center> <p>This is some other text. </p> </body>";

I was expecting text within <center>...</center> tag to be center aligned. But it's not happening!. Any way other than CSS ?

Anyway I don't want to set NSTextAlignmentCenter attribute to UILabel.

Thanks in advance...

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
byJeevan
  • 3,728
  • 3
  • 37
  • 60
  • From http://stackoverflow.com/questions/1798817/why-is-the-center-tag-deprecated-in-html I got know that `
    ` tag is removed in HTML5. Hence this issue !?
    – byJeevan Apr 28 '15 at 09:11
  • See the Link 1 . http://iosdevelopertips.com/objective-c/create-nsattributedstring-html-ios-7.html 2. http://stackoverflow.com/questions/28816501/nsattributedstring-alignment-not-working-on-html-content – Ashok Londhe Apr 28 '15 at 09:41
  • Able to fix by this replacement : `
    ...
    ` for `
    ` tag.
    – byJeevan Aug 20 '15 at 03:43

1 Answers1

1

Solved by replacing <center> tag with

<div style="text-align:center">...</div>

from w3schools, is no more supported in HTML5

enter image description here

byJeevan
  • 3,728
  • 3
  • 37
  • 60