gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:29:12 25 4
gpt4 key购买 nike

我正在使用 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()

这是它的显示方式:

simulator

关于html - UITextView html 文本 - 从项目中引用字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com