-
Notifications
You must be signed in to change notification settings - Fork 147
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
base: develop
Are you sure you want to change the base?
Changes from all commits
380523a
4f27745
08c85ec
2b59a4d
9568191
49a5925
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,9 @@ class LayoutManager: NSLayoutManager { | |
/// | ||
var blockquoteBorderWidth: CGFloat = 2 | ||
|
||
/// The list indent style | ||
/// | ||
var listIndentStyle: TextList.IndentStyle = .default | ||
|
||
/// Draws the background, associated to a given Text Range | ||
/// | ||
|
@@ -213,15 +216,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) | ||
|
@@ -233,8 +237,24 @@ private extension LayoutManager { | |
start = textStorage.numberOfItems(in: list, at: enclosingRange.location) | ||
} | ||
} | ||
|
||
var indentLevel = 1 | ||
// Determine indentation level, if needed. The indentation level is only used by the standard list style | ||
if list.style == .unordered, listIndentStyle == .standard { | ||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: The |
||
drawItem(markerString, in: markerRect, styled: attributes, at: enclosingRange.location) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,32 @@ 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
|
||
} | ||
} | ||
} | ||
|
||
/// List Indent Styles | ||
/// | ||
public enum IndentStyle: Int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Since this only applies to unordered lists, we should consider making this clearer. |
||
/// A default single bullet style for each indentation level | ||
case `default` | ||
/// The standard bullet styles for each indentation level (i.e., HTML style) | ||
case standard | ||
} | ||
|
||
public let reversed: Bool | ||
|
||
public let start: Int? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,17 @@ open class TextView: UITextView { | |
|
||
var maximumListIndentationLevels = 7 | ||
|
||
/// The list indent style | ||
/// Default is `default`, single style for each level. | ||
public var listIndentStyle: TextList.IndentStyle { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Since this only applies to unordered lists, we should consider making this clearer. |
||
get { | ||
return layout.listIndentStyle | ||
} | ||
set { | ||
layout.listIndentStyle = newValue | ||
} | ||
} | ||
|
||
// MARK: - Properties: Blockquotes | ||
|
||
/// The max levels of quote indentation allowed | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Since this only applies to unordered lists, we should consider making this clearer.