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

[WIP] Nested list implementation #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 17 additions & 3 deletions TextKit2NumberedBulletList/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ struct ContentView: View {
@StateObject var model = TextEditorModel()
@State private var text = NSAttributedString()
@State var showList = false
@State var nestItems = false
var body: some View {
VStack {
CustomTextEditor(text: $text, model: model)
Button("Ordered list: \(model.isParagraphActive.description)") {
showList.toggle()
HStack {
Button("Ordered list: \(model.isParagraphActive.description)") {
showList.toggle()
}
.buttonStyle(.bordered)
Button("Nested: \(model.isNestedParagraph.description)") {
nestItems.toggle()
}
.buttonStyle(.bordered)
}
.buttonStyle(.bordered)
}
.onChange(of: showList) {
if model.isParagraphActive {
Expand All @@ -26,6 +33,13 @@ struct ContentView: View {
model.setNumberedParagraph()
}
}
.onChange(of: nestItems) {
if model.isNestedParagraph {
model.unnestParagraph()
} else {
model.nestParagraph()
}
}
.padding()
}
}
Expand Down
2 changes: 2 additions & 0 deletions TextKit2NumberedBulletList/CustomTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension TextEditorModel: UITextViewDelegate {
// Check if there's any text selected
guard selectedRange.length > 0 else {
isParagraphActive = (textView.typingAttributes[.paragraphStyle] as? NSParagraphStyle)?.textLists.isNotEmpty ?? false
isNestedParagraph = (textView.typingAttributes[.paragraphStyle] as? NSParagraphStyle)?.textLists.last?.isOrdered == false
return
}

Expand All @@ -43,6 +44,7 @@ extension TextEditorModel: UITextViewDelegate {

// Check if the attributes contain a paragraph style
isParagraphActive = (attributes?[.paragraphStyle] as? NSParagraphStyle)?.textLists.isNotEmpty ?? false
isNestedParagraph = ((attributes?[.paragraphStyle] as? NSParagraphStyle)?.textLists.last?.isOrdered == false)
}

public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
Expand Down
13 changes: 13 additions & 0 deletions TextKit2NumberedBulletList/TextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@ import SwiftUI
public class TextEditorModel: NSObject, ObservableObject {

@Published var isParagraphActive: Bool = false
@Published var isNestedParagraph: Bool = false

weak var textView: CustomTextView?
func setNumberedParagraph() {
let list = NSTextList(markerFormat: .decimal, options: 0)

let listParagraph = NSMutableParagraphStyle()
listParagraph.paragraphSpacing = 0
listParagraph.lineSpacing = 15
listParagraph.textLists = [list]
listParagraph.alignment = .left


textView!.typingAttributes[.paragraphStyle] = listParagraph

textView!.setAttributes([.paragraphStyle: listParagraph], forRange: textView!.selectedRange)
}

func nestParagraph() {
let nestedList = NSTextList(markerFormat: .square, options: 0)
(textView?.typingAttributes[.paragraphStyle] as? NSMutableParagraphStyle)?.textLists.append(nestedList)
}

func unnestParagraph() {
let paragraph = textView?.typingAttributes[.paragraphStyle] as? NSMutableParagraphStyle
paragraph?.textLists.removeAll { !$0.isOrdered }
}

func unsetNumberedParagraph() {
textView?.typingAttributes[.paragraphStyle] = nil
textView?.removeAttributes(in: textView!.selectedRange, forAttribute: .paragraphStyle)
Expand Down