I’m using MBS to create attributedStrings from RTF. The RTF contains native hypertext links, and I want to convert them to Markdown links. Ideally, I would be able to use a data detector with AttributedStrings to collect and array of hits, but I haven’t found a way to do that. Alternatively, I could do a regex search, but I don’t see that as an option in MBS. I think I can iterate through each character and ask if it’s in a link, but before resorting to that I thought I’d ask if there is a better way. I only need a solution for macOS. What would be the best way to find native macOS links and replace them with Markdown links?
you use NSAttributedStringMBS class?
The text there has attribute NSLinkAttributeName with the link.
So you could loop through text with
method attributeAtIndex2(name as string, location as UInt64, byref effectiveRange as NSRangeMBS) as Variant
to start at location 0, then move forward for all the ranges you get and find all the links.
Here is a bit of sample code:
// list all links
Dim n As NSAttributedStringMBS = TextArea1.NSTextViewMBS.textStorage
Dim pos As Integer = 0
Dim Len As Integer = n.Length
Dim effectiveRange As NSRangeMBS
While pos < Len
Dim v As Variant = n.attributeAtIndex2(n.NSLinkAttributeName, pos, effectiveRange)
If v <> Nil Then
Dim url As String = v
Listbox1.AddRow url
End If
pos = pos + effectiveRange.Length
Wend
Yes, I’m using NSAttributedStringMBS. I was experimenting and came up with a looping method using attributeAtIndex – I see now that attributeAtIndex2 is what I should be using. Thank for the tip and the example!