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

时间:2016-10-04 07:45:53

标签: html ios swift uitextview nsattributedstring

我正在使用HTMLUITextView设置NSAttributedString文字。除了字体系列和颜色外,所有标签都有效。字体系列是OpenSans,它不是提到的{strong> 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

1 个答案:

答案 0 :(得分:2)

您只需将 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()

这是它的观点:

simulator