I am setting HTML
text for UITextView
using NSAttributedString
. All tags are working except for font family and color. Font family is OpenSans which is not Microsoft supported font as mentioned here. So I guess I need to refer OpenSans.ttf placed in the project directory for setting font. Not sure how to set that. This is the html string :
let htmlString = "<HTML><body style='font-color=rgba(74, 143, 137, 1)>The Breakthrough Course is designed with varied learning preferences in mind. You will view over 40 training videos; engage in interactive learning exercises, read supplemental texts, and capture important concepts with our tailored Note Taking Guide. You will see parents from a recent in-person class “get inside the child’s world” as they participate in experiential activities. You will learn the root causes of behavior (and misbehavior) and discover over 25 positive and helpful tools from the Positive Parenting Solutions Tool Box.</br></br><b>Navigating the Course:</b> The course design is as follows:</br></br><ul><li>Introduction</li><li>Sessions 1-6</li><li>Wrap-up and Next Steps</li></ul></br></br>You will also find resources to support your learning experience in the upper right-hand corner of each Session Page:</br></br><ul><li>Session</li><li>Note Taking Guide</li><li>Cumulative Tool Box</li><li>Session FAQ’s (Collected from parent questions asked over the years in our in-person classes.)</li><li>Index of Terms (This will direct you to the locations throughout the course where you can find information on a particular topic.)<li></br></br>The Breakthrough Course is designed for you to learn at your own pace. You can review the video content and the interactive learning exercises multiple times to improve your retention. That’s the beauty of an on-line learning experience!</br></br>We strongly recommend that you proceed through the course in-order. The principles and tools in each session build on the previous session. The earlier sessions lay the foundation so you understand what really motivates child behavior and the root cause of misbehavior. Once you understand the root cause, you can be confident that you are applying the appropriate tools to change the behavior for the long-term.</br></br>Don’t worry – you aren’t going to have to wait until the 3rd or 4th session to begin seeing results! You will see behavioral improvements right away when you begin implementing the first tool introduced in Session 1.</body></HTML>"
let attributedString = try? NSAttributedString(data: (htmlString.data(using: String.Encoding.unicode))!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
text_course_title.attributedText = attributedString
Unfortunately the html is coming as a single line here in the question, so I request you to copy paste it in a text editor for viewing it. Even the font color is not working. It shows a blank UITextView
if I apply font color as above.
You just need to add OpenSans.ttf file to your project, then set the font-family
and color
.
This would be the code (in Swift 2):
let htmlString = "<html><body style='font-family:OpenSans; color: rgba(74, 143, 137, 1)'>The Breakthrough Course ..."
let attributedString = try? NSAttributedString(data: htmlString.dataUsingEncoding(NSUTF8StringEncoding,
allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
textView.attributedText = attributedString
textView.sizeToFit()
This is how it would view: