Skip to content

Commit 153460e

Browse files
committed
Adjust drawing area of marker in lists
1 parent 1a9e6d6 commit 153460e

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

Aztec/Classes/TextKit/LayoutManager.swift

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private extension LayoutManager {
228228
else {
229229
return
230230
}
231-
231+
let attributes = textStorage.attributes(at: enclosingRange.location, effectiveRange: nil)
232232
let glyphRange = self.glyphRange(forCharacterRange: enclosingRange, actualCharacterRange: nil)
233233
let markerRect = rectForItem(range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
234234
var markerNumber = textStorage.itemNumber(in: list, at: enclosingRange.location)
@@ -240,8 +240,8 @@ private extension LayoutManager {
240240
}
241241
}
242242
markerNumber += start
243-
244-
drawItem(number: markerNumber, in: markerRect, from: list, using: paragraphStyle, at: enclosingRange.location)
243+
let markerString = list.style.markerText(forItemNumber: markerNumber)
244+
drawItem(markerString, in: markerRect, styled: attributes, at: enclosingRange.location)
245245
}
246246
}
247247

@@ -278,31 +278,36 @@ private extension LayoutManager {
278278
/// Draws the specified List Item Number, at a given location.
279279
///
280280
/// - Parameters:
281-
/// - number: Marker Number of the item to be drawn
281+
/// - markerText: Marker String of the item to be drawn
282282
/// - rect: Visible Rect in which the Marker should be rendered
283-
/// - list: Associated TextList
284-
/// - style: ParagraphStyle associated to the list
283+
/// - styled: Paragraph attributes associated to the list
285284
/// - location: Text Location that should get the marker rendered.
286285
///
287-
private func drawItem(number: Int, in rect: CGRect, from list: TextList, using style: ParagraphStyle, at location: Int) {
288-
guard let textStorage = textStorage else {
286+
private func drawItem(_ markerText: String, in rect: CGRect, styled paragraphAttributes: [NSAttributedString.Key: Any], at location: Int) {
287+
guard let style = paragraphAttributes[.paragraphStyle] as? NSParagraphStyle else {
289288
return
290289
}
291-
292-
let paragraphAttributes = textStorage.attributes(at: location, effectiveRange: nil)
293290
let markerAttributes = markerAttributesBasedOnParagraph(attributes: paragraphAttributes)
294-
295-
let markerPlain = list.style.markerText(forItemNumber: number)
296-
let markerText = NSAttributedString(string: markerPlain, attributes: markerAttributes)
291+
let markerAttributedText = NSAttributedString(string: markerText, attributes: markerAttributes)
297292

298293
var yOffset = CGFloat(0)
294+
var xOffset = CGFloat(0)
295+
let indentWidth = style.firstLineHeadIndent
296+
let markerWidth = markerAttributedText.size().width
299297

300298
if location > 0 {
301299
yOffset += style.paragraphSpacingBefore
302300
}
301+
// If the marker width is larger than the indent available let's offset the area to draw to the left
302+
if markerWidth > indentWidth {
303+
xOffset = indentWidth - markerWidth
304+
}
305+
306+
var markerRect = rect.offsetBy(dx: xOffset, dy: yOffset)
307+
308+
markerRect.size.width = max(indentWidth, markerWidth)
303309

304-
let markerRect = rect.offsetBy(dx: 0, dy: yOffset)
305-
markerText.draw(in: markerRect)
310+
markerAttributedText.draw(in: markerRect)
306311
}
307312

308313

@@ -328,7 +333,7 @@ private extension LayoutManager {
328333
}
329334

330335

331-
/// Returns the Marker Paratraph Attributes
336+
/// Returns the Marker Paragraph Attributes
332337
///
333338
private func markerParagraphStyle(indent: CGFloat) -> NSParagraphStyle {
334339
let tabStop = NSTextTab(textAlignment: .right, location: indent, options: [:])

0 commit comments

Comments
 (0)