NSAttributedString not working for UILabel after using HTML code

nsattributedstring bold swift
nsmutableparagraphstyle
change font of attributed string swift
bold & non-bold text in a single uilabel
nsattributedstring swift
uilabel with different font sizes swift
nsattributedstring font swift 4
nsattributedstring add attribute

I have UILabel and I am assigning html text as below.

m014.text = [NSString stringWithFormat:@"<html><head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 0px;padding: 0px;font-family: '%@'; font-size: %fpx;;ccolor:;}</style></head><body dir='%@'>%@<style type='text/css'> iframe { width: 100%% !important; } img {width: 100%% !important; height: auto;} table {width: 100%%;}</style></body></html>", localize(@"myFontName"), @16, localize(@"myDir"), m014.text];

m014.attributedText = [[NSAttributedString alloc] initWithData: [m014.text dataUsingEncoding:NSUTF32StringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil];

In iOS 10 & below I can see normal text (plain text) however in iOS 11 & above I see html text in the app. I see html code as it is...

Any idea why this is happening?


Majorly, I feel problem is in below line.

[m014.text dataUsingEncoding:NSUTF32StringEncoding]

But I remember, I used NSUTF32StringEncoding because I was seeing encoded characters.

Did you try NSUTF8StringEncoding when converting a HTML string to NSAttributtedString?

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[yourHTMLString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];

UILabel, Label displaying an attributed string, with overridden text color Specify the maximum number of lines for the label to use when laying out the text with the If you provide Auto Layout constraints that define the width of the label but not the  NSAttributedString HTML Additions. This project aims to duplicate the methods present on Mac OSX which allow creation of NSAttributedString from HTML code on iOS. This is useful for drawing simple rich text like any HTML document without having to use a UIWebView.

I'm using similar code in my app, but use NSUTF16StringEncoding, following the hints in some other answers, like this: NSAttributedString initWithHTML incorrect character encoding?.

NSAttributedString conversion: Bold, <b>- |Apple Developer Forums, NSAttributedString conversion: Bold, <b>-tag, not working The strings I'm showing in a UILabel consists of different html-tags, like <i>, <b>,  All these code samples are written to work with a Swift playground, which is a particularly good place to try out attributed strings because Xcode can render a preview of how the string looks. LEARN SWIFTUI FOR FREE I have a massive, free SwiftUI video collection on YouTube teaching you how to build complete apps with SwiftUI – check it out!

Thanks to @rmaddy for an answer in comment.

Update code I used is as below.

m014.attributedText = [[NSMutableAttributedString alloc] initWithHTML:
    [m014.text dataUsingEncoding:NSUTF8StringEncoding] 
    options: @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
    NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
    documentAttributes:nil];

NSAttributedString by example – Hacking with Swift, to want to use them with UILabel and UITextView , both of which accept All these code samples are written to work with a Swift playground, When that runs​, click the preview button for attributedQuote and you For example, we could define some HTML using inline CSS to make a paragraph blue: DTCoreText. This project aims to duplicate the methods present on Mac OSX which allow creation of NSAttributedString from HTML code on iOS. The project covers two broad areas: Layouting - Interfacing with CoreText, generating attributed strings from HTML code

Multiple Font, Style for UILable Text using NSAttributed String., As sometimes writing code, we stuck at situation like, some bold & normal text at single phrase, eg: “Hello, Code is Working.” or some cases like Intiate NSMutableAttributedString , with multiple style property & assign to label.​attributed text. YES ! <label>.attributedText = attributedText. Adding more properties like:. How to create rich formatted text strings using NSAttributedString. Attributed strings are strings with formatting attached, which means fonts, colors, alignment, line spacing and more. They are supported in many places around iOS, which means you can assign a fully formatted string to a UILabel and have it look great with no further work.

TTTAttributedLabel/TTTAttributedLabel: A drop-in , A drop-in replacement for UILabel that supports attributes, data detectors, GitHub is home to over 40 million developers working together to host and review code, and attributed text: just pass an NSString or NSAttributedString to the setText: detect links when the label text is subsequently changed label.​delegate = self  Simplifies working with attributed strings by allowing you to use HTML to describe formatting behaviors. NSAttributedString+DDHTML isn't intended to support full HTML rendering. Instead it provides a quick, effient and light-weight implementation for leveraging attributed strings when utilizing native UIKit interface elements.

How to display HTML in UITextView/UILabel with custom color, font , When you want to show a text from HTML content from a website to your We're not going to cover how to add a custom font to your iOS project in this to convert to NSAttributedString and place it like the example below:  Contribute to codepath/objc_ios_guides development by creating an account on GitHub. Document your code. Generating NSAttributedString from HTML.

Comments
  • 1. Why not use initWithHTML:documentAttributes: and 2. Try using NSUTF8StringEncoding
  • @rmaddy : Point 1 tell me my mistake (I believe)... :)
  • yes and I had issues, so went ahead with NSUTF32StringEncoding
  • What issues did you have?
  • I tried with this NSString and it worked. [NSString stringWithFormat:@"<html><head><style type='text/css'>body {background-color: transparent;border: 0px;margin: 0px;padding: 0px; font-size: 16px;;ccolor:;}</style></head><body >%@<style type='text/css'> iframe { width: 100%% !important; } img {width: 100%% !important; height: auto;} table {width: 100%%;}</style></body></html>", _labelView.text]; Can you explain what issue you had?
  • did you try with arabic texT? & iOS 11?
  • I tried with English. I will try with arabic and let you know.