Search code examples
htmliosiphonensattributedstring

NSAttributed string with `center` tag is not working


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...


Solution

  • Solved by replacing <center> tag with

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

    from w3schools, is no more supported in HTML5

    enter image description here