将文本中的URL链接着色为蓝色,而文本的其余部分保持为黑色。

huangapple go评论61阅读模式
英文:

coloring url links inside of text blue while rest of text black

问题

我要创建一个结构体,它返回文本。它应该检测文本中是否嵌套了链接,并且在文本被点击时打开链接。我还希望文本中的特定链接以蓝色显示,而文本的其余部分默认为颜色方案。我已经让检测工作正常,并且链接在点击时可以打开,但我无法让链接显示为蓝色,而且我收到了“类型'URL'的值没有成员'startIndex'”的错误。我认为前景修饰符不允许这样使用。如果有人知道如何使这种行为正常工作,我将不胜感激。

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)
                }
            }
    }
}
英文:

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) -&gt; 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..&lt;url.endIndex, in: text))
            .onTapGesture {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
            }
        
    }
}

答案1

得分: 1

它在Swift中运行

let string2 = "通过登录,您同意我们的"
let attributedString2 = NSMutableAttributedString(string: string2, attributes:[:])
let string3 = "和"
let attributedString3 = NSMutableAttributedString(string: string3, attributes:[:])

let string = "条款和条件"
let attributedString = NSMutableAttributedString(string: string, attributes:[NSAttributedString.Key.link: URL(string:"https://您的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 = "隐私政策"
let attributedString1 = NSMutableAttributedString(string: string1, attributes:[NSAttributedString.Key.link: URL(string: "https://您的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
英文:

its working in swift

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

        
        
 let string = &quot;Terms and Conditions &quot;
 let attributedString = NSMutableAttributedString(string: string, attributes:[NSAttributedString.Key.link: URL(string:&quot;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 = &quot;Privacy Policy&quot;
let attributedString1 = NSMutableAttributedString(string: string1, attributes:[NSAttributedString.Key.link: URL(string: &quot;https://Your URL&quot;)!, 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

huangapple
  • 本文由 发表于 2023年5月25日 10:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定