调用初始化程序时更改属性值

A.R*_*Roe 5 ios swift

这是我在此处发现的上一个问题的后续跟进

我正在使用MarkdownTextView向a添加基本降价UITextView.它TextView是的子类MarkdownTextView.

在我使用复制和粘贴时的上一个问题中,我收到以下错误

致命错误:对MarkdownTextStorage类使用未实现的初始化程序'init()'

直到我将以下初始化程序添加到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)
}
Run Code Online (Sandbox Code Playgroud)

但是,一旦我这样做,那么每次复制和粘贴时都会出现以下错误.

在此输入图像描述

似乎每次用户复制并粘贴初始化程序都会MarkdownAttributes在更改为设置的字体属性之前设置默认值ViewController.此屏幕截图位于正在设置的文本属性之间.

这就是我TextStorageViewController设置文本属性时使用的方法

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)
Run Code Online (Sandbox Code Playgroud)

这是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() {

}
Run Code Online (Sandbox Code Playgroud)

}

有没有人知道如何修复bug,所以当调用初始化器时,值被设置为用户定义的值而不是默认值?

Mos*_*bah 0

在您的代码中,您在使用时显式地使用默认属性进行初始化MarkdownAttributes(),您应该将 init 方法替换为如下所示:

public override convenience init() {
    var attributes = MarkdownTextAttributes()
    attributes.strongAttributes = [
        NSForegroundColorAttributeName: UIColor.redColor()
        // Add other attributes here
    ]
    self.init(attributes: attributes)
}
Run Code Online (Sandbox Code Playgroud)