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.

Thank you for the ‘more constructive answer’, I appreciate it.

The problem is that the HTML source has already been generated by the user in the past. I don't see a format-accurate way to convert the existing HTML directly into Markdown and then into an NSAttributedString. I originally opened a codelevel support case, was then referred to the forum and shared feedback accordingly. Is it likely that something else will happen via one of these routes. As far as I know, I can only submit app updates with iOS SDK 17 until the end of March and after that I would have to build with iOS SDK 18 then user content would no longer be displayed correctly.

I’ve been trying to work with Markdown, but I’ve had no success so far. Could you please let me know if I’ve done something wrong or if there’s anything specific I should try?

I’ve included the code I’ve been working on below. Any feedback or tips you can provide would be greatly appreciated.

Thank you for your help!

Here is the code :

    NSString *markdownString = @"Es ist wichtig, dass Sie Ihren Arzt informieren:\n\n "
                               @"    1. **HIER !!!!!!** Wenn Sie gegenwärtig mit Zometa®, dass die gleiche aktive Substanz enthält wie Aclasta® behandelt werden.\n\n"
                               @"    - **Hier !!!!!!** Wenn Sie Nierenbeschwerden haben oder hatten, denn Ihre Nieren müssen korrekt funktionieren, um das überflüssige Aclasta® das für Ihre Knochen nicht benötigt wird, ausscheiden zu können.\n\n"
                               @"    • Wenn sie Medikamente einnehmen, die Kortsion als Bestandteil enthalten (z. B. Prendisolon oder Dexamethason)\n\n"
                               @"    - Wenn sie unter schlechter Zahngesundheit oder Zahnfleischerkrankungen leiden oder wenn eine Zahnextraktion geplant ist";

    NSAttributedString *string = Nil;
    if (@available(iOS 14.0, *))
    {
           
            string = [[NSAttributedString alloc] initWithMarkdownString:markdownString options:nil baseURL:nil error:nil];
          
    }
    
    self.label.numberOfLines = 0;
    self.label.attributedText = string;
Basic HTML converted to NSAttributedString is not rendered correctly
 
 
Q