204 lines
6.1 KiB
Swift
204 lines
6.1 KiB
Swift
//
|
||
// RichText.swift
|
||
// AIGrammar
|
||
//
|
||
// Created by oscar on 2024/4/3.
|
||
//
|
||
|
||
import SwiftUI
|
||
|
||
|
||
struct RichText: View {
|
||
var text1: AttributedString {
|
||
var text = AttributedString(localized:"登录即表示同意")
|
||
text.foregroundColor = .gray
|
||
return text
|
||
}
|
||
var text2: AttributedString {
|
||
var text = AttributedString(localized:"用户协议")
|
||
text.link = URL(string: "111")
|
||
text.foregroundColor = .red
|
||
return text
|
||
}
|
||
var text3: AttributedString {
|
||
var text = AttributedString(localized:"和")
|
||
text.foregroundColor = .gray
|
||
return text
|
||
}
|
||
var text4: AttributedString {
|
||
var text = AttributedString(localized:"隐私协议")
|
||
text.link = URL(string: "222")
|
||
text.foregroundColor = .red
|
||
return text
|
||
}
|
||
|
||
var text: AttributedString {
|
||
text1 + text2 + text3 + text4
|
||
}
|
||
|
||
|
||
var body: some View {
|
||
VStack {
|
||
|
||
Text(text)
|
||
.environment(\.openURL, OpenURLAction { url in
|
||
let path = url.absoluteString
|
||
if path.hasPrefix("111") {
|
||
print("111...")
|
||
} else if path.hasPrefix("222") {
|
||
print("222...")
|
||
}
|
||
return .handled
|
||
})
|
||
|
||
Text("Device ID: \(globalEnvironment.deviceID)")
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
import UIKit
|
||
import SwiftUI
|
||
|
||
class RichTextViewController: UIViewController, UITextViewDelegate {
|
||
var textView: UITextView!
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
|
||
// 初始化 UITextView 并设置代理
|
||
textView = UITextView(frame: self.view.bounds)
|
||
textView.delegate = self
|
||
|
||
// 允许富文本交互
|
||
textView.isEditable = false
|
||
textView.isSelectable = true
|
||
|
||
// 创建富文本
|
||
let attributedString = NSMutableAttributedString(string: "点击这里进行测试")
|
||
|
||
// 设置点击部分的样式和链接
|
||
let linkAttributes: [NSAttributedString.Key: Any] = [
|
||
.link: URL(string: "http://baidu.com")!, // 使用自定义URL scheme
|
||
.foregroundColor: UIColor.blue
|
||
]
|
||
|
||
attributedString.setAttributes(linkAttributes, range: NSRange(location: 0, length: 4)) // 假设"点击这里"是可点击的
|
||
|
||
textView.attributedText = attributedString
|
||
|
||
// 将 UITextView 添加到当前视图
|
||
self.view.addSubview(textView)
|
||
}
|
||
|
||
// 处理富文本链接点击事件
|
||
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
|
||
print("特定文本部分被点击")
|
||
print(URL.scheme as Any)
|
||
if URL.scheme == "http" {
|
||
// 在这里添加点击后的处理逻辑
|
||
return false // 返回false表示不让系统处理这个URL
|
||
}
|
||
return true
|
||
}
|
||
}
|
||
|
||
struct RichTextView: UIViewRepresentable {
|
||
func makeUIView(context: Context) -> UITextView {
|
||
// 确保 RichTextViewController 实例在这里创建,并立即返回 textView
|
||
// 这样可以避免 textView 在使用之前未被初始化
|
||
let controller = RichTextViewController()
|
||
controller.loadViewIfNeeded() // 确保视图控制器的视图被加载,从而textView被初始化
|
||
return controller.textView
|
||
}
|
||
|
||
func updateUIView(_ uiView: UITextView, context: Context) {
|
||
// 更新UI视图(如果需要)
|
||
}
|
||
}
|
||
|
||
struct RichText: View {
|
||
var body: some View {
|
||
VStack{
|
||
Text("请点击下面的文本 请点击下面的文本 请点击下面的文本 请点击下面的文本 请点击下面的文本 请点击下面的文本 ")
|
||
// 使用我们的 RichTextView
|
||
RichTextView()
|
||
.frame(maxHeight: .infinity) // 设置一个合适的高度
|
||
//.edgesIgnoringSafeArea(.all) // 让视图延伸到屏幕的边缘
|
||
}
|
||
}
|
||
}
|
||
*/
|
||
|
||
/*
|
||
import SwiftUI
|
||
import UIKit
|
||
|
||
struct AttributedText: UIViewRepresentable {
|
||
var attributedString: NSAttributedString
|
||
|
||
func makeUIView(context: Context) -> UILabel {
|
||
let label = UILabel()
|
||
label.numberOfLines = 0 // 支持多行显示
|
||
label.attributedText = attributedString
|
||
|
||
// 添加手势识别器
|
||
let tapGesture = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.labelTapped(_:)))
|
||
label.addGestureRecognizer(tapGesture)
|
||
label.isUserInteractionEnabled = true
|
||
|
||
return label
|
||
}
|
||
|
||
func updateUIView(_ uiView: UILabel, context: Context) {
|
||
// 更新富文本字符串
|
||
uiView.attributedText = attributedString
|
||
}
|
||
|
||
func makeCoordinator() -> Coordinator {
|
||
Coordinator(self)
|
||
}
|
||
|
||
class Coordinator: NSObject {
|
||
var parent: AttributedText
|
||
|
||
init(_ parent: AttributedText) {
|
||
self.parent = parent
|
||
}
|
||
|
||
@objc func labelTapped(_ sender: UITapGestureRecognizer) {
|
||
// 处理点击事件,这里需要根据点击位置来确定用户点击的是哪一部分文本
|
||
// 这部分较为复杂,可能需要使用UILabel的子类来自定义实现
|
||
print("Label was tapped")
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
struct RichText: View {
|
||
var body: some View {
|
||
// 示例使用AttributedText
|
||
AttributedText(attributedString: attributedString)
|
||
}
|
||
|
||
var attributedString: NSAttributedString {
|
||
let fullString = NSMutableAttributedString(string: "Tap on ")
|
||
|
||
let clickablePart = NSAttributedString(string: "this text", attributes: [
|
||
.foregroundColor: UIColor.blue,
|
||
.underlineStyle: NSUnderlineStyle.single.rawValue
|
||
])
|
||
|
||
fullString.append(clickablePart)
|
||
fullString.append(NSAttributedString(string: " to see action."))
|
||
|
||
return fullString
|
||
}
|
||
}
|
||
|
||
*/
|
||
|
||
#Preview {
|
||
RichText()
|
||
}
|