UITextView html 文本 - 从项目中引用字体

UITextView html text - Refer font from project

我正在使用 NSAttributedStringUITextView 设置 HTML 文本。除字体系列和颜色外,所有标签均有效。字体系列是 OpenSans,它不是 Microsoft 支持的字体 ,如前所述 here。所以我想我需要参考放置在项目目录中的 OpenSans.ttf 来设置字体。不知道如何设置。这是 html 字符串:

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

不幸的是,html 在问题中以单行形式出现,因此我请求您将其复制粘贴到文本编辑器中以供查看。甚至字体颜色也不起作用。如果我如上所述应用字体颜色,它会显示空白 UITextView

你只需要添加OpenSans.ttf文件到你的项目,然后设置font-familycolor.

这将是代码(在 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()

这是它的显示方式: