malcommac / SwiftRichString

👩‍🎨 Elegant Attributed String composition in Swift sauce
MIT License
3.13k stars 211 forks source link

Fixed internal parser #22

Closed abumami closed 6 years ago

abumami commented 7 years ago

When I apply tags to a very large text, the attributes start getting applied to strange locations in text. I tried playing with this but haven't yet detected a pattern.

Question: Is there a size limit for the text being tagged?

abumami commented 7 years ago

The problem might be in my text. I tried a very large "lorem ipsum" text and it worked fine. I'm trying to figure out what the problem is with my text. I hope to either figure out what's wrong with my text, or to be able to reproduce a problematic scenario.

malcommac commented 7 years ago

If you have an example I'll be happy to check it thanks

toddvalentine commented 6 years ago

I am seeing the ranges a bit off as well? Here is my dummy text:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum  dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\"\r\n\r\n<bold>This is awesome!<\/bold>\r\n\r\n<store>appUrl here<\/store>\r\n\r\nLorem ipsum dolor sit amet, Monday, Jan. 30 at 3pm, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum  dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\""

Here is my code

let styleBold = Style("bold", {
            $0.font = FontAttribute(.Helvetica_Bold, size: 14)
        })

        let styleCenter = Style("center", {
            $0.align = .center
        })

        let styleStore = Style("store", {
            $0.color = UIColor(red: 255, green: 210, blue: 0, alpha: 1)
        })

        let def = Style.default {
            $0.font = FontAttribute(.Helvetica, size: 14)
            $0.color = UIColor(red: 255.00, green: 255.00, blue: 255.00, alpha: 1.00)
        }

        if let sourceTaggedString = article.body {
            let parser = MarkupString(source: sourceTaggedString, styles: [def, styleBold, styleCenter, styleStore])
            if let parsed = parser?.render(withStyles: [def, styleBold, styleCenter, styleStore]) {
                print(parsed)
                articleBody.attributedText = parsed
            }
        } else {
            articleBody.text = ""
        }

You'll see in the attached that the "here" which is wrapped within the tag is not styled as well as the "e!" which is wrapped within the tag.

screen shot 2018-01-23 at 6 06 40 am
malcommac commented 6 years ago

Problem has been fixed in 2.x branch where the parser has been rewritten. Attached the rendered version (with different colors) and the 2.x code (much simpler than before).

let styleBold = Style {
    $0.font = SystemFonts.Helvetica_Bold
    $0.size = 14
}

let styleCenter = Style {
    $0.alignment = .center
}

let styleStore = Style {
    $0.color = UIColor.red //UIColor(red: 255, green: 210, blue: 0, alpha: 1)
}

let def = Style {
    $0.font = SystemFonts.Helvetica
    $0.size = 14
    $0.color = UIColor.black //UIColor(red: 255.00, green: 255.00, blue: 255.00, alpha: 1.00)
}

let attributedString = body.set(style: StyleGroup(base: def, [
    "bold" : styleBold,
    "center" : styleCenter,
    "store" : styleStore
]))
screen shot 2018-09-03 at 20 12 48