Update attributedString in UITextVIew without changing origin attributedString

22 Views Asked by At

I am working to using UITextView to show RTF context,like an article, we usually see on webpage, in html style. including HEADING,PARAGRAPH, LIST or IMAGE, CODE_BLOCK BOLD etc.

the data format I get from api is like below

struct TextModel
{
   let text: String = "heading list item 1 item 2 paragraph, bold "
   let attributes: [Attribute]
}

struct Attribute 
{
   let type: Type = LIST
   let range Range = (start: 0, length: 10) 
}

enum Type 
{
  case LIST
  case LINK
  case BOLD
  case PARAGRAPH
   ....
}

so for the "heading list item 1 item 2 paragraph" I want to showing something like below

heading

list
  1. item 1 
  2. item 2
paragraph bold

but with no inserting extra \r or 1 2 number it will be show on textView like this

heading list item 1 item 2 paragraph bold

api 's text doesn't have \r or number, the logic api provide is if they pass to iOS a LIST or PARAGRAPH it will be a "block" text, so it should support line change automatically, also pass a LIST then iOS should add the 1,2,3... number at the beginning.

so basically I can set different attribute key value based on the Type the data returns but as you can see the above data has no \r or \n to do the linebreak, and the LIST Type doesn't have the order number 1,2,3... which need me to add into the NSAttributedString.

Originally I am planning to manually add these \r or \n or 1,2,3 but after inserting those, it will change the range data which api returns at the beginning, then I can't figure out what's the correct range for each TYPE. and to compute the updated range is very complicated.

so my question is without change the source string of the NSAttributedString, can I change the text during the time when I render the text through attributedString on UITextView?

is there any way I can change in NSTextStorage or NSLayoutManger or NSTextContainer basically mainly thing I need to add these linkbreak, \r or \n and show ordered list with 1,2,3 prefix without change origin data?

couple thoughts I have:

  1. is there a method I can put always put linebreak \r before certain index?
  2. can I change the textStorage text when call layout manager generate glyph?
0

There are 0 best solutions below