I have a UITextView
that contains paragraphs with text bullet lists (via NSTextList
). I also implement NSTextContentStorageDelegate.textContentStorage(_:, textParagraphWith:)
in order to apply some custom attributes to the text without affecting the underlying attributed text. My implementation returns a new NSParagraph
that modifies the foreground color of the text. I based this on the example in the WWDC 21 session "Meet Text Kit 2".
UITextView
stops rendering the bullets when I implement the delegate function and return a custom paragraph. Why?
func textContentStorage(_ textContentStorage: NSTextContentStorage, textParagraphWith range: NSRange) -> NSTextParagraph? {
guard let originalText = textContentStorage.textStorage?.attributedSubstring(from: range) else { return nil }
let updatedText = NSMutableAttributedString(attributedString: originalText)
updatedText.addAttribute(.foregroundColor, value: UIColor.green, range: NSRange(location: 0, length: updatedText.length))
let paragraph = NSTextParagraph(attributedString: updatedText)
// Verify that the text still contains NSTextList
if let paragraphStyle = paragraph.attributedString.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle {
assert(!paragraphStyle.textLists.isEmpty)
} else {
assertionFailure("Paragraph has lost its text lists")
}
return paragraph
}