psharanda / Atributika

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
MIT License
1.45k stars 155 forks source link

How to handle ordered lists? #95

Closed photos closed 5 years ago

photos commented 5 years ago

How should I handle <ol> ordered lists? Should I use the TagTransformer?

Example: <ol><li>Northerngiraffe</li><li>Reticulated giraffe</li><li>Masai giraffe</li><li>Southern giraffe</li></ol>

Expected Result: `1. Northern giraffe

  1. Reticulated giraffe
  2. Masai giraffe
  3. Southern giraffe`
psharanda commented 5 years ago

It depends from your desired result, but could be something like that

func stringWithOrderedList() -> NSAttributedString {
    var counter = 0
    let transformers: [TagTransformer] = [
        TagTransformer.brTransformer,
        TagTransformer(tagName: "ol", tagType: .start) { _ in
            counter = 0
            return ""
        },
        TagTransformer(tagName: "li", tagType: .start) { _ in
            counter += 1
            return "\(counter > 1 ? "\n" : "")\(counter). "
        }
    ]

    return "<ol><li>Coffee</li><li>Tea</li><li>Milk</li></ol>".style(tags: [], transformers: transformers).attributedString
}