Thanks for the reply, I have used a work around for this.
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!)