我正在使用 NSAttributedString 为 UITextView 设置 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-family 和color。
这将是代码(在 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()
这是它的显示方式:
关于html - UITextView html 文本 - 从项目中引用字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846860/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'