Saturday, October 14, 2023
HomeiOS Developmentios - Implementing numbered lists in UITextView utilizing Textual content Package 2...

ios – Implementing numbered lists in UITextView utilizing Textual content Package 2 API


I’m attempting to implement a nested numbered record in my textual content editor app like apple’s notes app does. I used to be in a position to obtain a few of it utilizing NSTextList in UITextView. I need to do nested numbered record however there appear to be no means out there. If I add indent to NSParagraphStyle which has NSTextList, it doesn’t work, Attributed String doesn’t replicate indent for some cause.

I got here throughout NSTextListElement watching WWDC 22 movies however there isn’t any documentation or pattern code which explains how one can implement it.

Are you able to inform me whether it is doable to do nested lists utilizing Textual content Package 2 in UITextView (UIKit)? If Sure, What’s the right method to implement it?

class ViewController: UIViewController {

var textView: UITextView!

override func viewDidLoad() {
tremendous.viewDidLoad()

textView = UITextView(usingTextLayoutManager: true)
textView.alwaysBounceVertical = true
textView.keyboardDismissMode = .interactive
textView.layer.borderColor = UIColor.black.cgColor
textView.layer.borderWidth = 2

view.addSubview(textView)

textView.translatesAutoresizingMaskIntoConstraints = false
[ textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
].forEach {
$0.isActive = true
}

self.loadText()
}

func loadText() {
let record = NSTextList(markerFormat: .decimal, choices: 0)
record.startingItemNumber = 1

let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.textLists = [list]

paragraphStyle.firstLineHeadIndent = 40
paragraphStyle.headIndent = 40

let attributes = [NSAttributedString.Key.paragraphStyle : paragraphStyle]

let nsmutableAttributedString = NSMutableAttributedString(string: "Good day World!", attributes: attributes)

textView.textStorage.setAttributedString(nsmutableAttributedString)

}
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments