FE_Tech
FE_Tech

Reputation: 1752

TextKit 2 : replaceContents(in:with:) is not working

Q1 :- I have NsTextList and it has [NsTextListElement], I want to replace an NsTextListElement with other element like NsTextParagraph or NstextListElement or an AttributedString. For some reason the below method is not working at all. And I couldn't find any alternate way of replacing the elements

textLayoutManager.replaceContents(in: element.elementRange, with: NSAttributedString(string: "happy"))

Q2 :- I am trying to do lists with the elements like NsTextList given by TextKit 2, Is it possible to do lists like this or should I do manually ?

Upvotes: -1

Views: 73

Answers (1)

FE_Tech
FE_Tech

Reputation: 1752

After researching about TextKit 2, I found out that some of APIs are not working and some are documented wrong.

  let paragraphElement = NSTextParagraph(attributedString: attrStr)
  textLayoutManager.replaceContents(in: element.elementRange, with: paragraphElement)
  

The above method is not working as documented, I found a work around for this problem by using other APIs of the TextKit 2.

    // creating an NSTextParagraph Element
    let paragraphElement = NSTextParagraph(attributedString: attrStr)

    //Getting the corresponding attributed String for the given element
    let attrString = textContentStorage.attributedString(for: paragraphElement)

    //replacing the attributed text using below method
    textContentStorage.textStorage?.replaceCharacters(in: nsrange, with: attrString!)

Upvotes: 0

Related Questions