1

I want to create a struct that returns Text. The text that it should return should detect whether a link is nested inside the text, and if so, open it when the text is tapped. I also want the specific link inside the text to be colored in blue while the rest of the text is default to the color Scheme. I got the detection working and the link to open on tap, but I can't get the link to be colored blue, and I am getting the error "Value of type 'URL' has no member 'startIndex'". I don't think the foreground modifier is allowed like this. I'd appreciate the help if anyone knows how to get this behavior to work

import Foundation
import UIKit
import SwiftUI

func coloredText(text: String) -> some View {
    let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let matches = detector?.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count))

    guard let url = matches?.first?.url else {
        return Text(text)
    }

    return VStack {
        Text(text)
            .foregroundColor(.black)
            .foregroundColor(.blue, range: NSRange(url.startIndex..<url.endIndex, in: text))
            .onTapGesture {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
            }
        
    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
ahmed
  • 341
  • 2
  • 9
  • the correct solution for this is found at: https://gist.github.com/mjm/0581781f85db45b05e8e2c5c33696f88 – ahmed May 25 '23 at 03:54
  • `url.startIndex.. `matches.first!.range` – Larme May 25 '23 at 08:23
  • You can do something like [this](https://stackoverflow.com/questions/76308018/tappable-substrings-in-swiftui/76308124#76308124) and turn them into proper markdown links – lorem ipsum May 25 '23 at 09:41

1 Answers1

1

its working in swift

let string2 = "By login, you agree to our "
let attributedString2 = NSMutableAttributedString(string: string2, attributes:[:])
let string3 = " and "
let attributedString3 = NSMutableAttributedString(string: string3, attributes:[:])

        
        
 let string = "Terms and Conditions "
 let attributedString = NSMutableAttributedString(string: string, attributes:[NSAttributedString.Key.link: URL(string:"https://Your URL” )!,NSAttributedString.Key.foregroundColor : UIColor.red])
        attributedString.addAttributes([ NSAttributedString.Key.font: UIFont.systemFont(ofSize: 22), NSAttributedString.Key.foregroundColor: UIColor.white], range:  NSRange(location: 0, length: attributedString.length))
        
        
let string1 = "Privacy Policy"
let attributedString1 = NSMutableAttributedString(string: string1, attributes:[NSAttributedString.Key.link: URL(string: "https://Your URL")!, NSAttributedString.Key.foregroundColor:  AppColors.PRIMARY])
        
        
              
let combination = NSMutableAttributedString()
combination.append(attributedString2)
combination.append(attributedString)
 combination.append(attributedString3)
 combination.append(attributedString1)
    
        
       
 txtlbl.attributedText = combination
        
 txtlbl.isUserInteractionEnabled = true
Radhe Yadav
  • 112
  • 11