Basic HTML converted to NSAttributedString is not rendered correctly

We have a sample code that initialises an NSAttributed string with simple HTML code and the result differs massively since XCode 16 and iPadOS 18 SDK.

DESCRIPTION OF PROBLEM Since Xcode Version 16.1 HTML content passed into an NSAttributedString is not rendered correctly. Specifically, enumeration characters (such as bullet points or numbers) and the proper indentation for text within list items are missing. As a result, the structure of the HTML content, especially ordered and unordered lists, is not displayed as expected.

STEPS TO REPRODUCE A UILabel was added to the view in a new Objective-C project in Xcode.

A HTML string containing <ul> <ol> and <li> Tags was converted into an NSAttributedString (With NSAttributedString::initWithData and NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType Parameter)within the viewDidLoad method and assigned to the UILabel.

Expected Result: The lists should be displayed correctly with enumeration characters and appropriate indentation, as shown in the attached screenshot expected_result.jpeg. Same Code still works as expected using XCode 15.x

Actual Result: The list is displayed without enumeration characters or indentation, which matches the attached screenshot actual_result.jpeg.

Sample Code

NSString *html = @"Es ist wichtig, dass Sie Ihren Arzt informieren:\n<ol>\n\t<li><strong>HIER !!!!!!! </strong>Wenn Sie gegenw&auml;rtig mit Zometa&reg;, dass die gleiche aktive Substanz enth&auml;lt wie Aclasta&reg; behandelt werden.</li>\n</ol>\n\n<ul>\n\t<li><strong>Hier !!!!!!</strong> Wenn Sie Nierenbeschwerden haben oder hatten, denn Ihre Nieren m&uuml;ssen korrekt funktionieren, um das &uuml;berfl&uuml;ssige Aclasta &reg; das f&uuml;r Ihre Knochen nicht ben&ouml;tigt wird, ausscheiden zu k&ouml;nnen.</li>\n\t<li>Wenn sie Medikamente einnehmen, die Kortsion als Bestandteil enthalten (z. B. Prendisolon oder Dexamethason)</li>\n\t<li>Wenn sie unter schlechter Zahngesundheit oder Zahnfleischerkrankungen leiden oder wenn eine Zahnextraktion geplant ist</li>\n</ul>\n";


NSString *textAttr = [NSString stringWithFormat:@"<span style=\"font-family:Arial; line-height: 80%%; font-size:10px;\">%@</style>", html];
NSData *data = [textAttr dataUsingEncoding:NSUTF16StringEncoding];

NSAttributedString *string = [[NSAttributedString alloc] initWithData:data
                                          options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                    NSCharacterEncodingDocumentAttribute: @(NSUTF16StringEncoding)}
                               documentAttributes:nil error:nil];

self.label.numberOfLines = 0;
self.label.attributedText = string;

Greetings

I don't think there is anything you can do to impact the HTML conversion of NSAttributedString unfortunately, and would suggest that you file a feedback report – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Just to make my above reply more constructive, if your input doesn't have to be HTML, you can probably consider using AttributedString + markdown.

You can create an AttributedString with a markdown string, as discussed in Instantiating Attributed Strings with Markdown Syntax. An AttributedString contains semantic markup (PresentationIntent), which you can convert to visual markup that controls how the text is rendered.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Basic HTML converted to NSAttributedString is not rendered correctly
 
 
Q