RichTextKit icon indicating copy to clipboard operation
RichTextKit copied to clipboard

Add support for links

Open danielsaidi opened this issue 3 years ago • 6 comments

It should be possible to add a link to the current selection, and remote the link if there is already one.

  • [ ] Add support for adding a link to the the current text position.
  • [ ] Add support for removing a link from the current text position.
  • [ ] Add support for detecting a link at the current text position.
  • [ ] Add standard icon
  • [ ] Add it to the demo

danielsaidi avatar Dec 10 '22 08:12 danielsaidi

Maybe this will give you a little head start 👍 It's what I'm using in my project right now to insert an autogenerated link.

    func insertLinkToBibleVerse() {
        
        let bibleBookFormatted = "\(currentBibleBookName()) \(bibleBook.chapter.sU):\(bibleBook.verse.sU)" // The name of the bible book to insert
        cursorPosition = context.selectedRange.location // Get the current position of the cursor for restoration as the last step
        let fontSize : [ NSAttributedString.Key : Any ] = [ .font : UIFont.systemFont(ofSize: 16.0 )  ]
        let oldNSAS = noteBlock.attributedNote  // Store the old string
        var linkAttribute : [ NSAttributedString.Key : Any ] = [ .link : NSURL(string: makeLinkToBibleVerse() )! ] //Create the new string to insert // Changed type to Any so that it could be merged to with other dictionary
        linkAttribute.merge( dict:  fontSize )
        
        let blankSpacesToInsert = NSAttributedString( string: " ", attributes: fontSize ) // Inserts a default context so that link does not expand after typing 
        let NSMSLink = NSMutableAttributedString( string: bibleBookFormatted, attributes: linkAttribute ) // Book string with link to insert into note..
        NSMSLink.append( blankSpacesToInsert )
        
        let combinedNSMAS = NSMutableAttributedString(attributedString: oldNSAS) // Add strings together
        combinedNSMAS.insert( NSMSLink, at: context.selectedRange.location )
        
        let spaceAfterScriptureIndex = cursorPosition + NSMSLink.string.count
        context.shouldSetAttributedString = combinedNSMAS // cannot use context.setAttributedString( to: ), will set font to context size..
        context.isEditingText = true
        
        if combinedNSMAS.length > spaceAfterScriptureIndex {
            context.pasteText("", at: spaceAfterScriptureIndex, moveCursorToPastedContent: true)
        }
        
    }
    
    func removeLink() {
        
        guard context.hasSelectedRange else { return }
        
        cursorPosition = context.selectedRange.location // Get the current position of the cursor for restoration as the last step
        let spaceAfterRangeIndex = cursorPosition  + 1
        let fontSize : [ NSAttributedString.Key : Any ] = [ .font : UIFont.systemFont(ofSize: 16.0 )  ]
        let blankSpacesToInsert = NSAttributedString( string: "   ", attributes: fontSize )
        
        let oldNSAS = noteBlock.attributedNote  // Store the old string
        let newNSMAS = NSMutableAttributedString(attributedString: oldNSAS) // Add strings together
        newNSMAS.append( blankSpacesToInsert )
        newNSMAS.removeAttribute(.link, range: context.selectedRange)
        
        context.shouldSetAttributedString = newNSMAS // cannot use context.setAttributedString( to: ), will set font to context size..
        context.isEditingText = true
        
        if newNSMAS.length > spaceAfterRangeIndex {
            context.pasteText("", at: spaceAfterRangeIndex, moveCursorToPastedContent: true) // blankSpacesToInsert is three spaces long, so this should never crash - Moves cursor position to one space after link
        }
        
    }

DavidAlvarezDev avatar Jun 25 '23 03:06 DavidAlvarezDev

Hi @DavidAlvarezDev

That's perfect, thank you! It would be amazing to add link support to the library. 👍

danielsaidi avatar Jun 29 '23 08:06 danielsaidi

Just wondering if link support has been added yet?

brendand avatar Feb 12 '25 22:02 brendand

So we just added this on our project and it works quite well. Happy to share. I dont think you'd actually want me to PR it officially as we've really customized a LOT in the project for our needs but maybe someone can take the one file and clean it up? https://github.com/CleftAI/RichTextKit/blob/30e525ab5a411192565091e1c012424a9d2d92c0/Sources/RichTextKit/Extensions/RichTextContext%2BLink.swift

I did add some customization to it to allow a linkcolor style to be passed in the editor style because I didn't like the default blue links.

itsthisjustin avatar Feb 13 '25 00:02 itsthisjustin

@itsthisjustin That works really well Justin! Thanks!

brendand avatar Feb 13 '25 19:02 brendand

Thank you @itsthisjustin 🙌 I will take a look at it and see if I can add it.

danielsaidi avatar Feb 18 '25 10:02 danielsaidi