这是我之前发现的问题 here 的跟进
我正在使用 MarkdownTextView将基本 Markdown 添加到 UITextView。 TextView 是 MarkdownTextView 的子类。
在我之前的问题中使用复制和粘贴时出现以下错误
Fatal error: Use of unimplemented initializer 'init()' for class MarkdownTextStorage
直到我将以下初始化程序添加到 MarkdownTextStorage 类
public override convenience init() {
self.init(attributes: MarkdownAttributes())
}
public init(attributes: MarkdownAttributes = MarkdownAttributes()) {
self.attributes = attributes
super.init()
commonInit()
if let headerAttributes = attributes.headerAttributes {
addHighlighter(MarkdownHeaderHighlighter(attributes: headerAttributes))
}
addHighlighter(MarkdownLinkHighlighter())
addHighlighter(MarkdownListHighlighter(markerPattern: "[*+-]", attributes: attributes.unorderedListAttributes, itemAttributes: attributes.unorderedListItemAttributes))
addHighlighter(MarkdownListHighlighter(markerPattern: "\\d+[.]", attributes: attributes.orderedListAttributes, itemAttributes: attributes.orderedListItemAttributes))
// From markdown.pl v1.0.1 <http://daringfireball.net/projects/markdown/>
// Code blocks
addPattern("(?:\n\n|\\A)((?:(?:[ ]{4}|\t).*\n+)+)((?=^[ ]{0,4}\\S)|\\Z)", attributes.codeBlockAttributes)
// Block quotes
addPattern("(?:^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+", attributes.blockQuoteAttributes)
// Se-text style headers
// H1
addPattern("^(?:.+)[ \t]*\n=+[ \t]*\n+", attributes.headerAttributes?.h1Attributes)
// H2
addPattern("^(?:.+)[ \t]*\n-+[ \t]*\n+", attributes.headerAttributes?.h2Attributes)
// Emphasis
addPattern("(\\*|_)(?=\\S)(.+?)(?<=\\S)\\1", attributesForTraits(.traitItalic, attributes.emphasisAttributes))
// Strong
addPattern("(\\*\\*|__)(?=\\S)(?:.+?[*_]*)(?<=\\S)\\1", attributesForTraits(.traitBold, attributes.strongAttributes))
// Inline code
addPattern("(`+)(?:.+?)(?<!`)\\1(?!`)", attributes.inlineCodeAttributes)
}
但是一旦我这样做了,每次复制和粘贴时我都会遇到以下错误。
似乎每次用户复制和粘贴时,初始化程序都会在更改为 ViewController 中设置的字体属性之前设置默认的 MarkdownAttributes。此屏幕截图位于正在设置的文本属性之间。
这就是我在 ViewController 中使用 TextStorage 来设置文本属性的方式
let fonty = UIFont(name: font, size: fsize)
attributes.defaultAttributes[NSFontAttributeName] = fonty
attributes.orderedListAttributes?[NSFontAttributeName] = fonty
attributes.orderedListItemAttributes?[NSFontAttributeName] = fonty
attributes.unorderedListAttributes?[NSFontAttributeName] = fonty
attributes.unorderedListItemAttributes?[NSFontAttributeName] = fonty
let textStorage = MarkdownTextStorage(attributes: attributes)
这是 MarkdownAttributes 结构
public struct MarkdownAttributes {
let fonty = UIFont(name: "OpenSans", size: 30)
public var defaultAttributes: TextAttributes = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
]
public var strongAttributes: TextAttributes?
public var emphasisAttributes: TextAttributes?
public struct HeaderAttributes {
public var h1Attributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
]
public var h2Attributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
]
public var h3Attributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
]
public var h4Attributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline)
]
public var h5Attributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline)
]
public var h6Attributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline)
]
func attributesForHeaderLevel(_ level: Int) -> TextAttributes? {
switch level {
case 1: return h1Attributes
case 2: return h2Attributes
case 3: return h3Attributes
case 4: return h4Attributes
case 5: return h5Attributes
case 6: return h6Attributes
default: return nil
}
}
public init() {}
}
public var headerAttributes: HeaderAttributes? = HeaderAttributes()
fileprivate static let MonospaceFont: UIFont = {
let bodyFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
let size = bodyFont.pointSize
return UIFont(name: "Menlo", size: size) ?? UIFont(name: "Courier", size: size) ?? bodyFont
}()
public var codeBlockAttributes: TextAttributes? = [
NSFontAttributeName: MarkdownAttributes.MonospaceFont
]
public var inlineCodeAttributes: TextAttributes? = [
NSFontAttributeName: MarkdownAttributes.MonospaceFont
]
public var blockQuoteAttributes: TextAttributes? = [
NSForegroundColorAttributeName: UIColor.darkGray
]
public var orderedListAttributes: TextAttributes? = [
NSFontAttributeName: fontWithTraits(.traitBold, font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body))
]
public var orderedListItemAttributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),
NSForegroundColorAttributeName: UIColor.darkGray
]
public var unorderedListAttributes: TextAttributes? = [
NSFontAttributeName: fontWithTraits(.traitBold, font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body))
]
public var unorderedListItemAttributes: TextAttributes? = [
NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),
NSForegroundColorAttributeName: UIColor.darkGray
]
public init() {
}
有谁知道我该如何修复该错误,以便在调用初始化程序时将值设置为用户定义的值而不是默认值?
最佳答案
添加一个接受用户定义值的 init 函数,并调用您的类或您的父类(super class) init()(取决于您想要做什么..)
您应该只添加以下功能之一,您不想同时添加它们:
public override init(attributes: MarkdownAttributes = MarkdownAttributes()) {
super.init(attributes)
}
public override convenience init(attributes: MarkdownAttributes = MarkdownAttributes()) {
init(attributes: attributes)
}
要调用它,您现在可以使用上面显示的代码:
let fonty = UIFont(name: font, size: fsize)
attributes.defaultAttributes[NSFontAttributeName] = fonty
attributes.orderedListAttributes?[NSFontAttributeName] = fonty
attributes.orderedListItemAttributes?[NSFontAttributeName] = fonty
attributes.unorderedListAttributes?[NSFontAttributeName] = fonty
attributes.unorderedListItemAttributes?[NSFontAttributeName] = fonty
let textStorage = MarkdownTextStorage(attributes: attributes)
关于ios - 调用初始化程序时更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54102138/
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr