@@ -27,6 +27,9 @@ class LayoutManager: NSLayoutManager {
2727 ///
2828 var blockquoteBorderWidth : CGFloat = 2
2929
30+ /// The list indent style
31+ ///
32+ var listIndentStyle : TextList . IndentStyle = . default
3033
3134 /// Draws the background, associated to a given Text Range
3235 ///
@@ -213,15 +216,16 @@ private extension LayoutManager {
213216 }
214217
215218 let characterRange = self . characterRange ( forGlyphRange: glyphsToShow, actualGlyphRange: nil )
219+ var firstLevelWidth : CGFloat ?
216220
217221 textStorage. enumerateParagraphRanges ( spanning: characterRange) { ( range, enclosingRange) in
218-
219222 guard textStorage. string. isStartOfNewLine ( atUTF16Offset: enclosingRange. location) ,
220223 let paragraphStyle = textStorage. attribute ( . paragraphStyle, at: enclosingRange. location, effectiveRange: nil ) as? ParagraphStyle ,
221224 let list = paragraphStyle. lists. last
222225 else {
223226 return
224227 }
228+
225229 let attributes = textStorage. attributes ( at: enclosingRange. location, effectiveRange: nil )
226230 let glyphRange = self . glyphRange ( forCharacterRange: enclosingRange, actualCharacterRange: nil )
227231 let markerRect = rectForItem ( range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
@@ -233,8 +237,22 @@ private extension LayoutManager {
233237 start = textStorage. numberOfItems ( in: list, at: enclosingRange. location)
234238 }
235239 }
240+
241+ var indentLevel : Int ?
242+ // Determine indentation level, if needed. The indentation level is only determined for the standard list style
243+ if listIndentStyle == . varied {
244+ // only get the width of the first level once
245+ if firstLevelWidth == nil {
246+ firstLevelWidth = paragraphStyle. indentToFirst ( TextList . self)
247+ }
248+
249+ // calculate current indent level
250+ let indentWidth = paragraphStyle. indentToLast ( TextList . self)
251+ indentLevel = Int ( indentWidth / firstLevelWidth!)
252+ }
253+
236254 markerNumber += start
237- let markerString = list. style. markerText ( forItemNumber: markerNumber)
255+ let markerString = list. style. markerText ( forItemNumber: markerNumber, indentLevel : indentLevel )
238256 drawItem ( markerString, in: markerRect, styled: attributes, at: enclosingRange. location)
239257 }
240258 }
0 commit comments