英文:
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) -> 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)
}
}
}
}
答案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 = "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论