Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any mechanism for knowing the links for the corresponding formatted text after the text has been formatted? #30

Open
christianselig opened this issue Dec 16, 2014 · 1 comment

Comments

@christianselig
Copy link

If I have a bunch of links that AttributedMarkdown is to parse, such as [Github](http://github.com), is there any mechanism for linking the formatted text that just becomes "GitHub", to the URL it's linked to?

Basically, I'm building a system allowing you to tap on the formatted text to open the corresponding URL, but I'm not sure how to get this URL once the text has been formatted.

Are there any recommendations for doing so?

(Apologies if this isn't actually an "issue", so to speak.)

@andrewgotow
Copy link

I was having this same problem, and figured it out just now. When AttributedMarkdown parses your links, it adds a custom attribute to the attributed string with the key attributedMarkdownURL, who's corresponding value is an NSURL containing the URL for your link.

I ended up enumerating the attributes on the string after it was parsed, and replaced the attributedMarkdownURL attributes with NSLinkAttributes, so that UITextViews can appropriately handle the links.

var attributedString : NSMutableAttributedString = markdown_to_attr_string( markdown, 0, _attributes )

attributedString.enumerateAttribute( "attributedMarkdownURL", inRange: NSRange(location: 0,length: attributedString.length), options: NSAttributedStringEnumerationOptions.allZeros) { (attributeValue, range, stop) -> Void in
    if attributeValue != nil {
        attributedString.addAttribute( NSLinkAttributeName, value: attributeValue!, range: range )
    }
 }

I know this is an old question, but I hope this helps you, and anyone else with this issue!
-Andrew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants