@@ -67,7 +67,7 @@ private extension LayoutManager {
6767
6868 enumerateLineFragments ( forGlyphRange: blockquoteGlyphRange) { ( rect, usedRect, textContainer, glyphRange, stop) in
6969
70- let startIndent = paragraphStyle. blockquoteIndent
70+ let startIndent = paragraphStyle. indentToFirst ( Blockquote . self ) - Metrics . listTextIndentation
7171
7272 let lineRange = self . characterRange ( forGlyphRange: glyphRange, actualGlyphRange: nil )
7373 let lineCharacters = textStorage. attributedSubstring ( from: lineRange) . string
@@ -78,13 +78,12 @@ private extension LayoutManager {
7878
7979 let nestDepth = paragraphStyle. blockquoteNestDepth
8080 for index in 0 ... nestDepth {
81- let indent = startIndent + CGFloat ( index) * Metrics. listTextIndentation
81+ let indent = paragraphStyle . indent ( to : index, of : Blockquote . self ) - Metrics. listTextIndentation
8282
83- let nestRect = self . blockquoteRect ( origin: origin, lineRect: rect, blockquoteIndent: indent, lineEndsParagraph: lineEndsParagraph)
83+ let nestRect = self . blockquoteRect ( origin: origin, lineRect: rect, blockquoteIndent: indent, lineEndsParagraph: lineEndsParagraph)
8484
85- self . drawBlockquoteBorder ( in: nestRect. integral, with: context, at: index)
85+ self . drawBlockquoteBorder ( in: nestRect. integral, with: context, at: index)
8686 }
87-
8887
8988 }
9089 }
@@ -100,7 +99,7 @@ private extension LayoutManager {
10099 return
101100 }
102101
103- let extraIndent = paragraphStyle. blockquoteIndent
102+ let extraIndent = paragraphStyle. indentToLast ( Blockquote . self )
104103 let extraRect = blockquoteRect ( origin: origin, lineRect: extraLineFragmentRect, blockquoteIndent: extraIndent, lineEndsParagraph: false )
105104
106105 drawBlockquoteBackground ( in: extraRect. integral, with: context)
@@ -121,11 +120,7 @@ private extension LayoutManager {
121120 private func blockquoteRect( origin: CGPoint , lineRect: CGRect , blockquoteIndent: CGFloat , lineEndsParagraph: Bool ) -> CGRect {
122121 var blockquoteRect = lineRect. offsetBy ( dx: origin. x, dy: origin. y)
123122
124- guard blockquoteIndent != 0 else {
125- return blockquoteRect
126- }
127-
128- let paddingWidth = Metrics . listTextIndentation * 0.5 + blockquoteIndent
123+ let paddingWidth = blockquoteIndent
129124 blockquoteRect. origin. x += paddingWidth
130125 blockquoteRect. size. width -= paddingWidth
131126
@@ -228,7 +223,7 @@ private extension LayoutManager {
228223 else {
229224 return
230225 }
231-
226+ let attributes = textStorage . attributes ( at : enclosingRange . location , effectiveRange : nil )
232227 let glyphRange = self . glyphRange ( forCharacterRange: enclosingRange, actualCharacterRange: nil )
233228 let markerRect = rectForItem ( range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
234229 var markerNumber = textStorage. itemNumber ( in: list, at: enclosingRange. location)
@@ -240,8 +235,8 @@ private extension LayoutManager {
240235 }
241236 }
242237 markerNumber += start
243-
244- drawItem ( number : markerNumber , in: markerRect, from : list , using : paragraphStyle , at: enclosingRange. location)
238+ let markerString = list . style . markerText ( forItemNumber : markerNumber )
239+ drawItem ( markerString , in: markerRect, styled : attributes , at: enclosingRange. location)
245240 }
246241 }
247242
@@ -278,42 +273,44 @@ private extension LayoutManager {
278273 /// Draws the specified List Item Number, at a given location.
279274 ///
280275 /// - Parameters:
281- /// - number : Marker Number of the item to be drawn
276+ /// - markerText : Marker String of the item to be drawn
282277 /// - rect: Visible Rect in which the Marker should be rendered
283- /// - list: Associated TextList
284- /// - style: ParagraphStyle associated to the list
278+ /// - styled: Paragraph attributes associated to the list
285279 /// - location: Text Location that should get the marker rendered.
286280 ///
287- private func drawItem( number : Int , in rect: CGRect , from list : TextList , using style : ParagraphStyle , at location: Int ) {
288- guard let textStorage = textStorage else {
281+ private func drawItem( _ markerText : String , in rect: CGRect , styled paragraphAttributes : [ NSAttributedString . Key : Any ] , at location: Int ) {
282+ guard let style = paragraphAttributes [ . paragraphStyle ] as? ParagraphStyle else {
289283 return
290284 }
291-
292- let paragraphAttributes = textStorage. attributes ( at: location, effectiveRange: nil )
293285 let markerAttributes = markerAttributesBasedOnParagraph ( attributes: paragraphAttributes)
294-
295- let markerPlain = list. style. markerText ( forItemNumber: number)
296- let markerText = NSAttributedString ( string: markerPlain, attributes: markerAttributes)
286+ let markerAttributedText = NSAttributedString ( string: markerText, attributes: markerAttributes)
297287
298288 var yOffset = CGFloat ( 0 )
289+ var xOffset = CGFloat ( 0 )
290+ let indentWidth = style. indentToLast ( TextList . self)
291+ let markerWidth = markerAttributedText. size ( ) . width * 1.5
299292
300293 if location > 0 {
301294 yOffset += style. paragraphSpacingBefore
302295 }
296+ // If the marker width is larger than the indent available let's offset the area to draw to the left
297+ if markerWidth > indentWidth {
298+ xOffset = indentWidth - markerWidth
299+ }
300+
301+ var markerRect = rect. offsetBy ( dx: xOffset, dy: yOffset)
302+
303+ markerRect. size. width = max ( indentWidth, markerWidth)
303304
304- let markerRect = rect. offsetBy ( dx: 0 , dy: yOffset)
305- markerText. draw ( in: markerRect)
305+ markerAttributedText. draw ( in: markerRect)
306306 }
307307
308308
309309 /// Returns the Marker Text Attributes, based on a collection that defines Regular Text Attributes.
310310 ///
311311 private func markerAttributesBasedOnParagraph( attributes: [ NSAttributedString . Key : Any ] ) -> [ NSAttributedString . Key : Any ] {
312312 var resultAttributes = attributes
313- var indent : CGFloat = 0
314- if let style = attributes [ . paragraphStyle] as? ParagraphStyle {
315- indent = style. listIndent + Metrics. listTextIndentation - ( Metrics . listTextIndentation / 4 )
316- }
313+ let indent : CGFloat = CGFloat ( Metrics . tabStepInterval)
317314
318315 resultAttributes [ . paragraphStyle] = markerParagraphStyle ( indent: indent)
319316 resultAttributes. removeValue ( forKey: . underlineStyle)
@@ -328,13 +325,14 @@ private extension LayoutManager {
328325 }
329326
330327
331- /// Returns the Marker Paratraph Attributes
328+ /// Returns the Marker Paragraph Attributes
332329 ///
333330 private func markerParagraphStyle( indent: CGFloat ) -> NSParagraphStyle {
334- let tabStop = NSTextTab ( textAlignment : . right , location : indent , options : [ : ] )
331+
335332 let paragraphStyle = NSMutableParagraphStyle ( )
336-
337- paragraphStyle. tabStops = [ tabStop]
333+ paragraphStyle. alignment = . right
334+ paragraphStyle. tailIndent = - indent
335+ paragraphStyle. lineBreakMode = . byClipping
338336
339337 return paragraphStyle
340338 }
0 commit comments