MarkdownKit
MarkdownKit copied to clipboard
List Identation
I was trying this library and faced the issue that lists doesn't have the correct indentation, like when the content of a line from a list breaks line, the second line starts below the "●" indicator.
What I would expect is to start below of the first character fo the first line
@cpvbruno as a temporary solution I've changed formatText
code to this in my custom MarkDownList
class. Indentation now works as expected. I'm not sure why original implementation avoids using paragraph style in lists, because this is obviously right approach.
class MyMarkdownList: MarkdownList {
override func formatText(_ attributedString: NSMutableAttributedString, range: NSRange, level: Int) {
var string = (0..<level).reduce("") { (string, _) -> String in
return "\(string)\(separator)"
}
string = "\(string)\(indicator) "
let paragraphStyle = NSMutableParagraphStyle()
let nonOptions = [NSTextTab.OptionKey: Any]()
paragraphStyle.tabStops = [
NSTextTab(textAlignment: .left, location: 20, options: nonOptions)]
paragraphStyle.defaultTabInterval = 20
paragraphStyle.headIndent = 10
paragraphStyle.lineSpacing = 0.5
paragraphStyle.paragraphSpacing = 12
attributedString.addAttributes(
[NSAttributedString.Key.paragraphStyle : paragraphStyle],
range: NSMakeRange(0, attributedString.length))
attributedString.replaceCharacters(in: range, with: string)
}
}
}
I am facing the same problem as @cpvbruno describes. I'm using v1.6.0 of this repository.
Also... how can I apply the solution @keylook has described? I've tried the following:
let parser = MarkdownParser(font: font, color: UIColor.black)
parser.addCustomElement(MyMarkdownList())
return parser
What am I missing here? Any help is greatly appreciated.
You must disable the link
element when initializing the MarkdownParser
, because the same regex is used to match the custom element and the link
element.
@t651330 Could you provide an example?