Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alternate bullet style per level #1354

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Aztec/Classes/TextKit/LayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,16 @@ private extension LayoutManager {
}

let characterRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
var firstLevelWidth: CGFloat?

textStorage.enumerateParagraphRanges(spanning: characterRange) { (range, enclosingRange) in

guard textStorage.string.isStartOfNewLine(atUTF16Offset: enclosingRange.location),
let paragraphStyle = textStorage.attribute(.paragraphStyle, at: enclosingRange.location, effectiveRange: nil) as? ParagraphStyle,
let list = paragraphStyle.lists.last
else {
return
}

let attributes = textStorage.attributes(at: enclosingRange.location, effectiveRange: nil)
let glyphRange = self.glyphRange(forCharacterRange: enclosingRange, actualCharacterRange: nil)
let markerRect = rectForItem(range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
Expand All @@ -233,8 +234,24 @@ private extension LayoutManager {
start = textStorage.numberOfItems(in: list, at: enclosingRange.location)
}
}

// determine indentation level
var indentLevel = 1
if list.style == .unordered {
// only get the width of the first level once
if firstLevelWidth == nil {
firstLevelWidth = paragraphStyle.indentToFirst(TextList.self)
}

// calculate current indent level
let indentWidth = paragraphStyle.indentToLast(TextList.self)
if let firstLevelWidth = firstLevelWidth {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd consider refactoring this since we know that at this point firstLevelWidth should never be nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

indentLevel = Int(indentWidth / firstLevelWidth)
}
}

markerNumber += start
let markerString = list.style.markerText(forItemNumber: markerNumber)
let markerString = list.style.markerText(forItemNumber: markerNumber, indentLevel: indentLevel)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The indentLevel isn't accurate if the list style isn't unordered + standard, which could cause confusion.

drawItem(markerString, in: markerRect, styled: attributes, at: enclosingRange.location)
}
}
Expand Down
15 changes: 12 additions & 3 deletions Aztec/Classes/TextKit/ParagraphProperty/TextList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ open class TextList: ParagraphProperty {
case ordered
case unordered

func markerText(forItemNumber number: Int) -> String {
func markerText(forItemNumber number: Int, indentLevel: Int = 1) -> String {
switch self {
case .ordered: return "\(number)."
case .unordered: return "\u{2022}"
case .ordered:
return "\(number)."
case .unordered:
switch indentLevel {
case 1:
return "\u{2022}"
case 2:
return "\u{2E30}"
default:
return "\u{2B29}"
}
cbess marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down