简体   繁体   中英

Why does my converted html string only show the first line?

I have made an attempt att converting html code to an attributed string that is then used in a label. But for some reason it only shows the very first line: "Before proceeding:" All else is blank. I have extended the label constraints to fill the entire screen to make sure there's no issue with that. This html snippet has been tested for android and works fine.

let mystring = """
        <body>
            <p><b>Before proceeding:</b></p>
            <ul type="dash">
                <li>Save a backup copy of the door configuration file</li><li>Disconnect all other devices</li></ul>
            <p><b>During upgrade:</b></p>
            <ul type="dash">
                <li>Do not turn off phone</li>
            </ul>
            <p><b>Are you sure you want to upgrade?</b></p>
            </body>
        """

var attrString: NSAttributedString? {
    guard let data = mystring.data(using: .utf8) else { return nil }
    do {
        return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
    } catch {
         return nil
    }
}
         
mylabel.attributedText = attrString

Is there anything that I have missed?

It may not be entirely obvious, but...

At first it would seem that the Number of Lines property of a UILabel allows word-wrapping when the text is long enough.

However...

The Number of Lines property of a UILabel also controls the number of lines displayed, even if the text has embedded newline characters .

Setting mylabel.numberOfLines = 0 will fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM