这是我上次发现问题here
后的跟进我正在使用MarkdownTextView向UITextView
添加基本减价。 TextView
是MarkdownTextView
的子类。
在上一个问题中,使用复制和粘贴时出现以下错误
致命错误:对类使用未实现的初始化程序'init()' 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)
}
但是,一旦这样做,每次复制粘贴时都会遇到以下错误。
似乎每次用户复制并粘贴初始化程序时,都会在更改为MarkdownAttributes
中设置的字体属性之前设置默认值ViewController
。该屏幕快照位于正在设置的文本属性之间。
这就是我在TextStorage
中使用ViewController
设置文本属性的方式
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() {
}
}
有人知道我该如何解决该错误,以便在调用初始化程序时将值设置为用户定义的值,而不是默认值?
答案 0 :(得分:4)
添加一个采用用户定义值的init函数,然后调用您的类或您的超类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)
答案 1 :(得分:0)
在代码中,使用MarkdownAttributes()
时将使用默认属性进行显式初始化,应将init方法替换为以下内容:
public override convenience init() {
var attributes = MarkdownTextAttributes()
attributes.strongAttributes = [
NSForegroundColorAttributeName: UIColor.redColor()
// Add other attributes here
]
self.init(attributes: attributes)
}